python docx | 表格元素垂直水平居中

时间:2025-02-07 21:50:05

安装

pip install python-docx

表格元素垂直水平居中

doc.tables[0].cell(a, b).vertical_alignment = WD_ALIGN_VERTICAL.CENTER # 垂直居中
doc.tables[0].cell(a, b).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

a,b 为表格元素的坐标

Document(path) 读取word

from docx import Document

docx  = Document("")

tables

docx.tables # 表格对象列表

table = docx.tables[0]
table.rows # 单个表格的所有行

row = table.rows[0] 
row.cells # 单行的所有列(单元格)

cell = row.cells[0]
cell.text # 单元格内容

paragraphs