使用 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')
我缺少显示所有样式可能性的方式。
但是你无法直接看到相应的结果,你需要编写一个循环来为每种样式生成一个表格。