如何摆脱python-docx生成的word文档中表对象的默认样式? [英] How do I get rid of the default styling on a table object in a python-docx generated word document?

查看:647
本文介绍了如何摆脱python-docx生成的word文档中表对象的默认样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.)

推荐答案

在当前版本中,此类事情的最佳选择是应用表格样式,例如:

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

table.style = 'LightShading-Accent1'

可以用您描述的方式修改Word中的表格样式,例如标头字体,行带状等.因此,您可以修改现有样式之一以适合或创建自己的样式.

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.

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

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

这篇关于如何摆脱python-docx生成的word文档中表对象的默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆