Python使用win32com模塊實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動(dòng)生成word表格的方法
本文實(shí)例講述了Python使用win32com模塊實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動(dòng)生成word表格的方法。分享給大家供大家參考,具體如下:
下載win32模塊
下載鏈接:https://sourceforge.net/projects/pywin32/files/pywin32/
連接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 #是否在后臺(tái)運(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ì)文檔 #不會(huì)用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
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python+MySQL數(shù)據(jù)庫程序設(shè)計(jì)入門教程》、《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用
- 解決python中導(dǎo)入win32com.client出錯(cuò)的問題
- Python操作word常見方法示例【win32com與docx模塊】
- Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能示例
- Python win32com 操作Exce的l簡單方法(必看)
- python使用win32com庫播放mp3文件的方法
- python字符串加密解密的三種方法分享(base64 win32com)
- Python 基于win32com客戶端實(shí)現(xiàn)Excel操作的詳細(xì)過程
相關(guān)文章
Pytorch中accuracy和loss的計(jì)算知識(shí)點(diǎn)總結(jié)
在本片文章里小編給大家整理的是關(guān)于Pytorch中accuracy和loss的計(jì)算相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-09-09Python3 Post登錄并且保存cookie登錄其他頁面的方法
今天小編就為大家分享一篇Python3 Post登錄并且保存cookie登錄其他頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12詳解Python中的array數(shù)組模塊相關(guān)使用
數(shù)組并不是Python中內(nèi)置的標(biāo)配數(shù)據(jù)結(jié)構(gòu),不過擁有array模塊我們也可以在Python中使用數(shù)組結(jié)構(gòu),下面我們就來詳解詳解Python中的array數(shù)組模塊相關(guān)使用2016-07-07Python實(shí)現(xiàn)讀取文件中的特定行的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在Python中實(shí)現(xiàn)讀取文件中的特定行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03python使用opencv進(jìn)行人臉識(shí)別
本文主要介紹了python使用opencv進(jìn)行人臉識(shí)別的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04