python模組win32com 實現資料庫表結構自動生成word表格
阿新 • • 發佈:2019-01-12
下載win32模組
連線mysql
import MySQLdbdb_host = ""db_port = 3306db_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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
獲取所有表結構
#獲取表資料庫中所有表和備註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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
win32com操作
from win32com.client import Dispatch,constantsword = Dispatch('Word.Application')word.Visible = 1 #是否在後臺執行wordword.DisplayAlerts = 0 #是否顯示警告資訊doc = word.Documents.Add() #新增一個文件r = doc.Range(0,0) #獲取一個範圍r.Style.Font.Name = u"Verdana" #設定字型r.Style.Font.Size = "9" #設定字型大小r.InsertBefore("\n" + 表描述 + " " + 表名) #在這個範圍前插入文字table = r.Tables.Add(doc.Range(r.End,r.End),欄位數+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"預設值"table.Rows[0].Cells[3].Range.Text = u"是否為空"table.Rows[0].Cells[4].Range.Text = u"列備註"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
完整程式碼
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow