欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用python生成mysql數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔

 更新時(shí)間:2022年01月20日 09:24:15   作者:henghenghahei_3  
大家好,本篇文章主要講的是用python生成mysql數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

最近因?yàn)轫?xiàng)目原因需要編寫(xiě)數(shù)據(jù)庫(kù)設(shè)計(jì)文檔,但是由于數(shù)據(jù)表太多,手動(dòng)編寫(xiě)耗費(fèi)的時(shí)間太久,所以搞了一個(gè)簡(jiǎn)單的腳本快速生成數(shù)據(jù)庫(kù)結(jié)構(gòu),保存到word文檔中。

安裝pymysql和document

pip install pymysql
pip install document

腳本

# -*- coding: utf-8 -*-
import pymysql
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn

db = pymysql.connect(host='127.0.0.1', #數(shù)據(jù)庫(kù)服務(wù)器IP
                         port=3306,
                         user='root',
                         passwd='123456',
                         db='test_db') #數(shù)據(jù)庫(kù)名稱)
#根據(jù)表名查詢對(duì)應(yīng)的字段相關(guān)信息
def query(tableName):
    #打開(kāi)數(shù)據(jù)庫(kù)連接
    cur = db.cursor()
    sql = "select b.COLUMN_NAME,b.COLUMN_TYPE,b.COLUMN_COMMENT from (select * from information_schema.`TABLES`  where TABLE_SCHEMA='test_db') a right join(select * from information_schema.`COLUMNS` where TABLE_SCHEMA='test_db_test') b on a.TABLE_NAME = b.TABLE_NAME where a.TABLE_NAME='" + tableName+"'"
    cur.execute(sql)
    data = cur.fetchall()
    cur.close
    return data
#查詢當(dāng)前庫(kù)下面所有的表名,表名:tableName;表名+注釋(用于填充至word文檔):concat(TABLE_NAME,'(',TABLE_COMMENT,')')
def queryTableName():
    cur = db.cursor()
    sql = "select TABLE_NAME,concat(TABLE_NAME,'(',TABLE_COMMENT,')') from information_schema.`TABLES`  where TABLE_SCHEMA='test_db_test'"
    cur.execute(sql)
    data = cur.fetchall()
    return data
#將每個(gè)表生成word結(jié)構(gòu),輸出到word文檔
def generateWord(singleTableData,document,tableName):
    p=document.add_paragraph()
    p.paragraph_format.line_spacing=1.5 #設(shè)置該段落 行間距為 1.5倍
    p.paragraph_format.space_after=Pt(0) #設(shè)置段落 段后 0 磅
    #document.add_paragraph(tableName,style='ListBullet')
    r=p.add_run('\n'+tableName)
    r.font.name=u'宋體'
    r.font.size=Pt(12)
    table = document.add_table(rows=len(singleTableData)+1, cols=3,style='Table Grid')
    table.style.font.size=Pt(11)
    table.style.font.name=u'Calibri'
    #設(shè)置表頭樣式
    #這里只生成了三個(gè)表頭,可通過(guò)實(shí)際需求進(jìn)行修改
    for i in ((0,'NAME'),(1,'TYPE'),(2,'COMMENT')):
        run = table.cell(0,i[0]).paragraphs[0].add_run(i[1])
        run.font.name = 'Calibri'
        run.font.size = Pt(11)
        r = run._element
        r.rPr.rFonts.set(qn('w:eastAsia'), '宋體')
    
    for i in range(len(singleTableData)):
        #設(shè)置表格內(nèi)數(shù)據(jù)的樣式
        for j in range(len(singleTableData[i])):
            run = table.cell(i+1,j).paragraphs[0].add_run(singleTableData[i][j])
            run.font.name = 'Calibri'
            run.font.size = Pt(11)
            r = run._element
            r.rPr.rFonts.set(qn('w:eastAsia'), '宋體')
        #table.cell(i+1, 0).text=singleTableData[i][1]
        #table.cell(i+1, 1).text=singleTableData[i][2]
        #table.cell(i+1, 2).text=singleTableData[i][3]
    

if __name__ == '__main__':
    #定義一個(gè)document
    document = Document()
    #設(shè)置字體默認(rèn)樣式
    document.styles['Normal'].font.name = u'宋體'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
    #獲取當(dāng)前庫(kù)下所有的表名信息和表注釋信息
    tableList = queryTableName()
    #循環(huán)查詢數(shù)據(jù)庫(kù),獲取表字段詳細(xì)信息,并調(diào)用generateWord,生成word數(shù)據(jù)
    #由于時(shí)間匆忙,我這邊選擇的是直接查詢數(shù)據(jù)庫(kù),執(zhí)行了100多次查詢,可以進(jìn)行優(yōu)化,查詢出所有的表結(jié)構(gòu),在代碼里面將每個(gè)表結(jié)構(gòu)進(jìn)行拆分
    for singleTableName in tableList:
        data = query(singleTableName[0])
        generateWord(data,document,singleTableName[1])
    #保存至文檔
    document.save('數(shù)據(jù)庫(kù)設(shè)計(jì).docx')

生成的word文檔預(yù)覽

在這里插入圖片描述

到此這篇關(guān)于用python生成mysql數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔的文章就介紹到這了,更多相關(guān)python生成mysql結(jié)構(gòu)文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論