python word 设置表格居中

from docx import Document


def align_table(doc_path):
# 打开文档
doc = Document(doc_path)

# 遍历文档中的所有表格
table = doc.tables[0]
# 遍历表格的行
for row in table.rows:
# 遍历行的单元格
for cell in row.cells:
# 将单元格的文字对齐方式设置为居中对齐
cell.paragraphs[0].alignment = 1
doc.save(doc_path)

# 使用函数将表格变整齐
align_table('cc.docx')
posted @ 2024-02-01 16:54  郭峰g  阅读(168)  评论(0编辑  收藏  举报