欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python word-Document Export

程序员文章站 2022-03-02 12:24:51
# 附上 python-docx 的官网链接 官网优详细dome 可以移步观看:https://python-docx.readthedocs.io/en/latest/index.html安装依赖:pip install python-docx在此附上个人dome (被注释掉的就是 dome ) from docx import Documentfrom docx.shared import Inchesclass exportDocument(): def...
# 附上 python-docx 的官网链接 官网优详细dome 可以移步观看:
https://python-docx.readthedocs.io/en/latest/index.html
  1. 安装依赖:
pip install python-docx

python word-Document Export

  1. 在此附上个人dome (被注释掉的就是 dome )
 
from docx import  Document
from docx.shared import Inches


class exportDocument():
    def __init__(self):
        self.outPutPath="D:/project/demo.docx"
        self.tableNames=[]

    def setOutPutPath(self,outPutPath):
        self.outPutPath=outPutPath

    def setTableNames(self, tableNames):
        self.tableNames = tableNames

    def printInfoMention(self):
        if len(self.tableNames)==0: return

        doc = Document()
        doc.add_heading('表结构文档', 0)
        p = doc.add_paragraph(' 表结构元数据信息 ')
        p.add_run('bold').bold = True
        p.add_run(' and some ')
        p.add_run('italic.').italic = True
 

        for val in self.tableNames:
            print("handler table name is : "+val)
            doc.add_heading(val, level=1)
            # 一行 4格
            table = doc.add_table(rows=1, cols=4)
            hdr_cells = table.rows[0].cells
            # hdr_cells.length =  cols=4
            hdr_cells[0].text = '字段ID'
            hdr_cells[1].text = '字段名称'
            hdr_cells[2].text = '字段类型'
            hdr_cells[3].text = '字段描述'
            self.setValues(table,val)
            doc.add_page_break()



        doc.save(self.outPutPath)

    def setValues(self, table,tableName):
    # 请求数据库   tableInfoMention().getinfomention(tableName)
  
        ret =(
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)
        for i, val in enumerate(ret, 0):
            row_cells = table.add_row().cells
            row_cells[0].text = val[0]
            row_cells[1].text = val[2]
            row_cells[2].text = val[1]
            row_cells[3].text = val[2]

# dome 
#if __name__ == "__main__":
   # out = exportDocument()
   # out.setTableNames([
        # "main_business_org_total_report",
        # "main_business_org_total_report",
   # ])
    # 自己设置路径
    # out.setOutPutPath();
   # out.printInfoMention()

本文地址:https://blog.csdn.net/weixin_43843042/article/details/110933807