直接上代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
import mysql.connector
from docx import Document
db = 'targetDB'
host = 'localhost'
user = 'username'
password = 'password'
port = 3306
conn = mysql.connector.connect(host=host, user=user, passwd=password, db=db, port=port)
cursor = conn.cursor()
sql = "select table_name,table_comment from information_schema.tables where table_schema = '{}'".format(db)
cursor.execute(sql)
document = Document()
for (table_name, table_comment) in cursor.fetchall():
document.add_heading(table_name, level=1)
document.add_paragraph(table_comment)
table = document.add_table(rows=1, cols=3, style='Table Grid')
hdr_cells = table.rows[0].cells
hdr_cells[0].text = u'字段'
hdr_cells[1].text = u'类型'
hdr_cells[2].text = u'说明'
cursor.execute("SHOW FULL FIELDS FROM {}.{}".format(db, table_name))
for (field, type, _, _, _, _, _, _, comment) in cursor.fetchall():
row_cells = table.add_row().cells
row_cells[0].text = field
row_cells[1].text = type
row_cells[2].text = comment
document.save('db.docx')
589



被折叠的 条评论
为什么被折叠?



