0

根据 python-docx 文档,以下代码生成一个包含三列的简单表:

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for item in recordset:
    row_cells = table.add_row().cells
    row_cells[0].text = str(item.qty)
    row_cells[1].text = str(item.id)
    row_cells[2].text = item.desc

但是,row_cells[0] 的默认样式是粗体。如何摆脱第一列的特殊样式?

(另外,如果我能摆脱默认的带状行,那也很棒。)

4

2 回答 2

0

在当前版本中,最好的办法是应用表格样式,例如:

table.style = 'LightShading-Accent1'

可以按照您描述的方式修改 Word 中的表格样式,例如标题字体、行带状等。因此您可以修改现有样式中的一种以适合或创建您自己的样式。

确保您研究了 python-docx 文档中的这两个页面,以了解如何在运行时使样式在您的文档中可用:

http://python-docx.readthedocs.org/en/latest/user/styles.html http://python-docx.readthedocs.org/en/latest/user/documents.html

于 2014-12-05T05:21:48.773 回答
0

您需要了解文档中的样式

styles = document.styles
for s in styles:
  print(s.name)

然后试试这个

table2 = document.add_table( rows=15, cols=15,style='Table Grid')

您可以将样式更改为您的文档支持的任何样式

(style='Table Grid')

python 3.4 文档 0.8.6

于 2017-04-13T07:24:52.323 回答