最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用win32com模塊實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動(dòng)生成word表格的方法

 更新時(shí)間:2018年07月17日 14:26:26   作者:jaysonhu  
這篇文章主要介紹了Python使用win32com模塊實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)自動(dòng)生成word表格的方法,結(jié)合實(shí)例形式分析了win32com模塊下載、連接mysql、查詢獲取表結(jié)構(gòu)以及使用win32com生成word表格的相關(guān)操作技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • 淺談numpy生成數(shù)組的零值問題

    淺談numpy生成數(shù)組的零值問題

    今天小編就為大家分享一篇淺談numpy生成數(shù)組的零值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python中scipy.stats產(chǎn)生隨機(jī)數(shù)實(shí)例講解

    python中scipy.stats產(chǎn)生隨機(jī)數(shù)實(shí)例講解

    在本篇文章里小編給大家分享的是一篇關(guān)于python中scipy.stats產(chǎn)生隨機(jī)數(shù)實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2021-02-02
  • Python爬蟲實(shí)戰(zhàn)之爬取某寶男裝信息

    Python爬蟲實(shí)戰(zhàn)之爬取某寶男裝信息

    網(wǎng)絡(luò)爬蟲是一種按照一定的規(guī)則自動(dòng)瀏覽、檢索網(wǎng)頁信息的程序或者腳本。網(wǎng)絡(luò)爬蟲能夠自動(dòng)請求網(wǎng)頁,并將所需要的數(shù)據(jù)抓取下來。本文將為大家介紹如何利用爬蟲獲取某寶男裝信息,感興趣的小伙伴可以了解一下
    2021-12-12
  • Django Admin后臺(tái)添加數(shù)據(jù)庫視圖過程解析

    Django Admin后臺(tái)添加數(shù)據(jù)庫視圖過程解析

    這篇文章主要介紹了Django Admin后臺(tái)添加數(shù)據(jù)庫視圖過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python自動(dòng)安裝第三方庫的小技巧(pip使用詳解)

    Python自動(dòng)安裝第三方庫的小技巧(pip使用詳解)

    很多朋友私信小編Python安裝第三方庫安裝技巧,在這就不一一回復(fù)大家了,今天小編給大家分享一篇教程關(guān)于Python自動(dòng)安裝第三方庫的小技巧,本文以安裝plotly為例給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Python解析pcap文件示例

    Python解析pcap文件示例

    這篇文章主要為大家介紹了Python解析pcap文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 用python畫個(gè)奧運(yùn)五環(huán)(附完整代碼)

    用python畫個(gè)奧運(yùn)五環(huán)(附完整代碼)

    大家好,本篇文章主要講的是用python畫個(gè)奧運(yùn)五環(huán)(附完整代碼),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • python將字典列表導(dǎo)出為Excel文件的方法

    python將字典列表導(dǎo)出為Excel文件的方法

    這篇文章主要介紹了python將字典列表導(dǎo)出為Excel文件的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python 開心網(wǎng)和豆瓣日記爬取的小爬蟲

    python 開心網(wǎng)和豆瓣日記爬取的小爬蟲

    我本科有個(gè)很幽默風(fēng)趣的量子力學(xué)老師,他說了很多批話,跟個(gè)公知似的。他的很多文章都放在了開心網(wǎng)(kaixin001.com)上,為了留個(gè)紀(jì)念,用爬蟲保存下來
    2021-05-05
  • python如何保證輸入鍵入數(shù)字的方法

    python如何保證輸入鍵入數(shù)字的方法

    今天小編就為大家分享一篇python如何保證輸入鍵入數(shù)字的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評(píng)論

临江市| 丰台区| 荣昌县| 马鞍山市| 八宿县| 昌都县| 广昌县| 密山市| 茶陵县| 巧家县| 慈溪市| 普定县| 新绛县| 章丘市| 麻栗坡县| 凌海市| 蚌埠市| 平远县| 新晃| 鸡泽县| 灵寿县| 绥滨县| 遂昌县| 增城市| 内黄县| 敦化市| 泌阳县| 乌拉特中旗| 大宁县| 抚宁县| 柳江县| 积石山| 台湾省| 会东县| 遂川县| 东源县| 高唐县| 克拉玛依市| 万源市| 曲靖市| 土默特右旗|