亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • Pytorch中accuracy和loss的計(jì)算知識(shí)點(diǎn)總結(jié)

    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-09
  • Python googletrans庫使用示例詳解

    Python googletrans庫使用示例詳解

    googletrans是一個(gè)基于谷歌翻譯API的Python庫,支持多種語言的自動(dòng)檢測(cè)和翻譯,提供了translate和detect方法,用于翻譯文本和檢測(cè)文本語言,通過簡單的命令即可安裝使用,適合需要實(shí)現(xiàn)多語言翻譯功能的開發(fā)者
    2024-09-09
  • Django如何配置mysql數(shù)據(jù)庫

    Django如何配置mysql數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了Django配置mysql數(shù)據(jù)庫的詳細(xì)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Python3 Post登錄并且保存cookie登錄其他頁面的方法

    Python3 Post登錄并且保存cookie登錄其他頁面的方法

    今天小編就為大家分享一篇Python3 Post登錄并且保存cookie登錄其他頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 詳解Python中的array數(shù)組模塊相關(guān)使用

    詳解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-07
  • Python實(shí)現(xiàn)讀取文件中的特定行的方法詳解

    Python實(shí)現(xiàn)讀取文件中的特定行的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何在Python中實(shí)現(xiàn)讀取文件中的特定行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 對(duì)于Python中線程問題的簡單講解

    對(duì)于Python中線程問題的簡單講解

    這篇文章主要介紹了對(duì)于Python中線程問題的簡單講解,線程一直是Python編程當(dāng)中的熱點(diǎn)問題,而本文沒有涉及GIL線程鎖方面的內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python標(biāo)準(zhǔn)庫之加密模塊詳解

    Python標(biāo)準(zhǔn)庫之加密模塊詳解

    這篇文章主要為大家詳細(xì)介紹了Python標(biāo)準(zhǔn)庫中加密模塊的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-07-07
  • python3 反射的四種基本方法解析

    python3 反射的四種基本方法解析

    這篇文章主要介紹了python3 反射的四種基本方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python使用opencv進(jìn)行人臉識(shí)別

    python使用opencv進(jìn)行人臉識(shí)別

    本文主要介紹了python使用opencv進(jìn)行人臉識(shí)別的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04

最新評(píng)論