# -*- coding:utf-8 -*-# 安装插件:pip install python-docxfrom docx import Documentfrom docx.shared import Cmfrom docx.enum.text import WD_ALIGN_PARAGRAPHfrom docx.enum.table import WD_ALIGN_VERTICALdocument = Document() # 创建文档对象# 创建1行4列带框线的表DocxTable1 = document.add_table(rows = 1, cols = 4, style = 'Table Grid')DocxTable1.cell(0, 0).text = '单元格内第一行' # 向单元格填入内容DocxTable1.cell(0, 0).add_paragraph('单元格内第二行') # 向单元格填入多段内容DocxTable1.cell(0, 1).text = '测试'                   # 居中属性测试DocxTable1.alignment = WD_ALIGN_PARAGRAPH.CENTER # 整个表格居中for r in range(1):                                    # 逐个单元格内容居中    DocxTable1.rows[r].height = Cm(1)                 # 设置行高为1厘米 for c in range(4):        # 第一段水平居中        DocxTable1.cell(r, c).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER        # 垂直居中        DocxTable1.cell(r, c).vertical_alignment = WD_ALIGN_VERTICAL.CENTERdocument.save('测试文档.docx')