python生成word文档的表格_如何摆脱python-docx生成的word文档中表对象的默认样式?...

1586010002-jmsa.png

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

解决方案

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值