Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成word表格的方法
本文實例講述了Python使用win32com模塊實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動生成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 #是否在后臺運行word
word.DisplayAlerts = 0 #是否顯示警告信息
doc = word.Documents.Add() #新增一個文檔
r = doc.Range(0,0) #獲取一個范圍
r.Style.Font.Name = u"Verdana" #設(shè)置字體
r.Style.Font.Size = "9" #設(shè)置字體大小
r.InsertBefore("\n" + 表描述 + " " + 表名) #在這個范圍前插入文本
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è)計文檔
#不會用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è)計入門教程》、《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python中scipy.stats產(chǎn)生隨機數(shù)實例講解
在本篇文章里小編給大家分享的是一篇關(guān)于python中scipy.stats產(chǎn)生隨機數(shù)實例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-02-02
Django Admin后臺添加數(shù)據(jù)庫視圖過程解析
這篇文章主要介紹了Django Admin后臺添加數(shù)據(jù)庫視圖過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04

