首页 > 数据库 >Python 生成MySQL数据库的表结构到word文档

Python 生成MySQL数据库的表结构到word文档

时间:2022-11-08 18:01:12浏览次数:33  
标签:word name Python text cells db column MySQL table

原理:读取系统表的数据,调用python-docx库生成word文档。

import pymysql
from docx import Document
from docx.shared import Inches


document = Document()
document.add_heading('数据库表结构', 0)

conn = pymysql.connect(host='192.168.1.17', user='root', passwd="password", db='db1')
cur = conn.cursor()
cur.execute('''SELECT
                    t.table_schema,
                    t.table_name,
                    t.column_name,
                    t.column_type,
                    t.is_nullable,
                    t.column_default,
                    t.column_comment     
                FROM
                    INFORMATION_SCHEMA.COLUMNS t 
                WHERE table_schema in('db1')
                ORDER BY table_schema,table_name''')
col_list = cur.fetchall()

db_tb_list = []
for table_schema,table_name,column_name,column_type, is_nullable,column_default,column_comment  in col_list: 
    result = [r for r in db_tb_list if r[0] == table_schema and r[1] == table_name]
    if(len(result) == 0):  
        db_tb_list.append((table_schema,table_name))

db_tmp = ''
for db,tb in db_tb_list:
    if (db_tmp != db):
        document.add_heading('数据库' + db, 1)
    db_tmp = db
    document.add_heading('表' + db + '.' + tb, 5)
 
    this_table = [r for r in col_list if r[0] == db and r[1] == tb]
    #添加表格:
    # 表格样式参数style可选:
    # Normal Table
    # Table Grid
    # Light Shading、 Light Shading Accent 1 至 Light Shading Accent 6
    # Light List、Light List Accent 1 至 Light List Accent 6
    # Light Grid、Light Grid Accent 1 至 Light Grid Accent 6
    # 其它省略...  
    table = document.add_table(rows=1, cols=5, style='Light Grid')
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = '列'
    hdr_cells[1].text = '类型'
    hdr_cells[2].text = '空'
    hdr_cells[3].text = '默认'
    hdr_cells[4].text = '说明'
    for table_schema,table_name,column_name,column_type, is_nullable,column_default,column_comment  in this_table:
        row_cells = table.add_row().cells       
        row_cells[0].text = column_name
        row_cells[1].text = column_type
        row_cells[2].text = '是' if is_nullable is 'YES' else '否'
        row_cells[3].text = '' if column_default is None else column_default
        row_cells[4].text = column_comment

document.save('数据库表结构.docx')

cur.close()
conn.close()

 

标签:word,name,Python,text,cells,db,column,MySQL,table
From: https://www.cnblogs.com/gdjlc/p/16870623.html

相关文章

  • QProcess 调用.py脚本(windows + python 环境)
    1QProcessp;2QStringListargs1;3args1.append("demo.py");//设置py脚本4p.setWorkingDirectory("d:/demo/");//设置py脚本所在目录5p......
  • Python driver 设置
    options=webdriver.ChromeOptions()#不加载图片,加快访问速度options.add_experimental_option("prefs",{"profile.managed_default_content_settings.images":2})#......
  • mysql根据json字段内容作为查询条件(包括json数组)检索数据
    最近用到了mysql5.7的json字段的检索查询,发现挺好用的,记录一下笔记我们有一个日志表,里面的data字段是保存不同对象的json数据,遇到想根据里面的json的字段内容作为条件查询......
  • 【MySQL】基础练习题
    习题参考:https://blog.csdn.net/qq_41936662/article/details/80393172数据库准备建表语句学生表studentCREATETABLE`student`(`s_id`varchar(20)NOTNULL,......
  • Python基础之面向对象:7、面向对象之魔法方法
    目录面向对象之魔法方法一、魔法方法的概念二、常用魔法方法1、__init__2、__str__3、__call__4、__getattr__5、__getattribute__6、__setattr__7、__enter......
  • python 面向对象之元类
    python面向对象之元类type方法的应用1.查看数据类型s1='helloworld'#str()l1=[11,22,33,44]#list()d1={'name':'jason','pwd':123}#dict()t1......
  • Python基础之面向对象:8、面向对象之元类
    目录面向对象之元类一、什么是元类二、元类推导流程三、创建类的方式方式一:方式二:四、元类定制类的产生行为五、元类定制对象的产生行为六、元类之双下new面向对象之元类......
  • dpkg 安装mysql
    名称版本系统Ubuntu16.04MySQL5.7.26下载安装包wgethttps://dev.mysql.com/get/Downloads/MySQL-8.mysql-server_8.0.16-2ubuntu18.04_amd64.deb-bun......
  • MySQL锁表解锁表
    CREATETABLEt1(idint(11)NOTNULL,valvarchar(10)DEFAULTNULL,PRIMARYKEY(id))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4;INSERTintot1(i......
  • MySQL主从复制
    MySQL主从复制主机名称IP备注mysql-master192.168.175.93mastermysql-slave192.168.175.130slave在mysql-master上创建一个复制账号;mysql-master......