How do I get rid of the default styling on a table object in a python-docx generated word document?

三世轮回 提交于 2019-12-24 22:32:26

问题


Per the python-docx documentation the following code generates a simple table with three columns:

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

However, the default styling of the row_cells[0] is bold. How do I get rid of the special styling on the first column?

(Also, if I could get rid of the default banded rows that would be great as well.)


回答1:


The best bet for this sort of thing in the current version is to apply a table style, for example:

table.style = 'LightShading-Accent1'

It's possible to modify the table styles in Word in the ways you describe, e.g. header font, row banding, etc. So you can modify one of the existing styles to suit or create your own.

Make sure you study these two pages in the python-docx documentation to understand how to make styles available in your document at run time:

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




回答2:


you need to know styles in your document

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

then try this

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

you can change style to whatever your document supports with

(style='Table Grid')

python 3.4 docx 0.8.6



来源:https://stackoverflow.com/questions/27307676/how-do-i-get-rid-of-the-default-styling-on-a-table-object-in-a-python-docx-gener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!