美文网首页
用python-docx对mysql表结构生成doc里表格的形式

用python-docx对mysql表结构生成doc里表格的形式

作者: 风吹路过的云 | 来源:发表于2021-06-08 10:48 被阅读0次

    有时,我们要对mysql表结构导出来,制作成doc里面的table形式,如下图

    table形式
    看了mysql的导出功能,无法满足,于是想到自己写个脚本来实现。百度搜了一把,发现有个叫python-docx的模块。
    python-docx的安装
    python的版本:3.7.9
    用pip3安装过程中,一直报错,装不了,只好下载.whl文件来安装,下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/
    我下载的文件是:python_docx-0.8.6-py2.py3-none-any.whl
    用下面的命令
    pip3 install python_docx-0.8.6-py2.py3-none-any.whl
    

    即可安装好,并成功使用
    下面是制作table的完整代码:

    #!/usr/bin/python3
     
    import pymysql
    from docx import Document
    from docx.shared import Inches
    from docx.enum.text import WD_ALIGN_PARAGRAPH
    from docx.enum.table import WD_ALIGN_VERTICAL
    from docx.oxml.ns import nsdecls
    from docx.oxml import parse_xml
     
    # 打开数据库连接
    db = pymysql.Connect(
        host='192.168.1.20',
        port=3306,
        user='root',
        passwd='xxxxxxxx',
        db='test_db',
        charset='utf8'
    )
    
    schema_name = input('数据库名:')
    table_name = input('表名,多个用空格隔开:')
    schema_name = schema_name.strip()
    table_name = table_name.strip()
    tns = table_name.split(" ")
     
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    document = Document()
    for t in tns:
        cursor.execute("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE,COLUMN_DEFAULT, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE  table_schema ='%s'  AND table_name='%s'" % (schema_name, t))
        document.add_paragraph()
        document.add_paragraph('表名:%s' % t)
        table = document.add_table(rows=1, cols=6)
        table.style = 'Table Grid'
        table.columns[0].width = Inches(0.49)
        hdr_cells = table.rows[0].cells
        hdr_cells[0].add_paragraph('字段名').alignment=WD_ALIGN_PARAGRAPH.CENTER
        hdr_cells[1].add_paragraph('数据类型').alignment=WD_ALIGN_PARAGRAPH.CENTER
        hdr_cells[2].add_paragraph('长度').alignment=WD_ALIGN_PARAGRAPH.CENTER
        hdr_cells[3].add_paragraph('非空').alignment=WD_ALIGN_PARAGRAPH.CENTER
        hdr_cells[4].add_paragraph('默认值').alignment=WD_ALIGN_PARAGRAPH.CENTER
        hdr_cells[5].add_paragraph('描述').alignment=WD_ALIGN_PARAGRAPH.CENTER
        for row in cursor.fetchall():
            row_cells = table.add_row().cells
            row_cells[0].text = row[0]
            row_cells[1].text = row[1]
            row_cells[2].text = str(row[2])
            row_cells[3].text = row[3]
            row_cells[4].text = str(row[4])
            row_cells[5].text = row[5]
        # 首行加底纹
        shading_list = locals()
        for i in range(6):
            shading_list['shading_elm_'+str(i)] = parse_xml(r'<w:shd {} w:fill="{bgColor}"/>'.format(nsdecls('w'),bgColor = 'cccccc'))
            table.rows[0].cells[i]._tc.get_or_add_tcPr().append(shading_list['shading_elm_'+str(i)])
    
    document.add_page_break()
    document.save('demo.docx')
    # 关闭数据库连接
    db.close()
    
    
    
    

    相关文章

      网友评论

          本文标题:用python-docx对mysql表结构生成doc里表格的形式

          本文链接:https://www.haomeiwen.com/subject/kgfzsltx.html