您好,登錄后才能下訂單哦!
怎么在Python中使用win32com模塊生成word表格?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
連接mysql
import MySQLdb db_host = "" db_port = 3306 db_name = "" db_user = "" db_pwd = "" db = MySQLdb.connect(host=db_host,port=db_port,user=db_user,passwd=db_pwd,db=db_name,charset="utf8") cursor = db.cursor()
獲取所有表結(jié)構(gòu)
#獲取表數(shù)據(jù)庫中所有表和備注 def get_tables(cursor,db_name): sql = "select table_name,table_comment from information_schema.tables where table_schema = '" + db_name + "'" cursor.execute(sql) result = cursor.fetchall() tables = {} for r in result: tables[r[0]] = r[1] return tables #獲取表結(jié)構(gòu) def get_table_desc(cursor,db_name,table_name): sql = "select column_name,column_type,column_default,is_nullable,column_comment from information_schema.columns where table_schema = '" + db_name + "' and table_name = '" + table_name + "'" cursor.execute(sql) result = cursor.fetchall() return result
win32com操作
from win32com.client import Dispatch,constants word = Dispatch('Word.Application') word.Visible = 1 #是否在后臺運(yùn)行word word.DisplayAlerts = 0 #是否顯示警告信息 doc = word.Documents.Add() #新增一個(gè)文檔 r = doc.Range(0,0) #獲取一個(gè)范圍 r.Style.Font.Name = u"Verdana" #設(shè)置字體 r.Style.Font.Size = "9" #設(shè)置字體大小 r.InsertBefore("\n" + 表描述 + " " + 表名) #在這個(gè)范圍前插入文本 table = r.Tables.Add(doc.Range(r.End,r.End),字段數(shù)+1,5) #建一張表格 table.Rows[0].Cells[0].Range.Text = u"列" table.Rows[0].Cells[1].Range.Text = u"類型" table.Rows[0].Cells[2].Range.Text = u"默認(rèn)值" table.Rows[0].Cells[3].Range.Text = u"是否為空" table.Rows[0].Cells[4].Range.Text = u"列備注"
完整代碼
#coding:utf-8 #把數(shù)據(jù)庫中的表結(jié)構(gòu)導(dǎo)出到word的表格中,完成設(shè)計(jì)文檔 #不會用win32com操作word樣式 import MySQLdb,config from win32com.client import Dispatch,constants db_name = "crawlerdb_update" db = MySQLdb.connect(host=config.db_host,port=config.db_port,user=config.db_user,passwd=config.db_pwd,db=db_name,charset="utf8") cursor = db.cursor() def get_tables(cursor,db_name): sql = "select table_name,table_comment from information_schema.tables where table_schema = '" + db_name + "'" cursor.execute(sql) result = cursor.fetchall() tables = {} for r in result: tables[r[0]] = r[1] return tables def get_table_desc(cursor,db_name,table_name): sql = "select column_name,column_type,column_default,is_nullable,column_comment from information_schema.columns where table_schema = '" + db_name + "' and table_name = '" + table_name + "'" cursor.execute(sql) result = cursor.fetchall() return result tables = get_tables(cursor,db_name) word = Dispatch('Word.Application') word.Visible = 1 word.DisplayAlerts = 0 doc = word.Documents.Add() r = doc.Range(0,0) r.Style.Font.Name = u"Verdana" r.Style.Font.Size = "9" for k,table_name in enumerate(tables): tables_desc = get_table_desc(cursor,db_name,table_name) print r.Start r.InsertBefore("\n" + tables[table_name] + " " + table_name) table = r.Tables.Add(doc.Range(r.End,r.End),len(tables_desc) + 1,5) table.Rows[0].Cells[0].Range.Text = u"列" table.Rows[0].Cells[1].Range.Text = u"類型" table.Rows[0].Cells[2].Range.Text = u"默認(rèn)值" table.Rows[0].Cells[3].Range.Text = u"是否為空" table.Rows[0].Cells[4].Range.Text = u"列備注" for i,column in enumerate(tables_desc): for j,col in enumerate(column): if col == None: col = "(NULL)" table.Rows[i+1].Cells[j].Range.Text = col r = doc.Range(table.Range.End,table.Range.End) break
看完上述內(nèi)容,你們掌握怎么在Python中使用win32com模塊生成word表格的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。