python-docx 表的可用样式列表

问题描述 投票:0回答:1

使用 python-docx 时有没有办法显示所有可用的表格样式?

在下面的示例中,我使用“表格网格”样式,但我想探索更多样式。

from docx import Document
doc = Document()

table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'
table.autofit = False
table.allow_autofit = False
for row in table.rows:
    for cell in row.cells:
        cell.width = Pt(100)
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Age'
table.cell(0, 2).text = 'City'
for i, data in enumerate([('Alice', '25', 'New York'), ('Bob', '30', 'San Francisco'), ('Charlie', '22', 'Los Angeles')], start=1):
    table.cell(i, 0).text = data[0]
    table.cel(i, 1).text = data[1]
    table.cell(i, 2).text = data[2]

doc.save('example_document.docx')

我缺少显示所有样式可能性的方式。

python python-docx
1个回答
0
投票

这里似乎有一个列表:https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#table-styles-in-default-template

但是你无法直接看到相应的结果,你需要编写一个循环来为每种样式生成一个表格。

© www.soinside.com 2019 - 2024. All rights reserved.