python 操作word文字加粗、变红_使用python-docx在表格中使单元格加粗

下面的代码段基本上创建了一个表,该表具有在新Word文档中所需的行数和列数,即2列和14行.然后,将内容相应地添加到行和列.

from docx import Document

newDoc=Document()

newDoc.add_heading ('GIS Request Form')

newDoc.add_paragraph()

#inserting a table and the header and value objects to the table

table=newDoc.add_table(rows=14,cols=2)

table.style='Table Grid'

table.autofit=False

table.columns[0].width=2500000

table.columns[1].width=3500000

#inserting contents into table cells

for i in range(0,14):

row=table.rows[i]

row.cells[0].text=reqdheaderList[i]

row.cells[1].text=reqdvalueList[i]

我一直在尝试使第1列中所有内容的内容变为粗体,但无法正常工作.

#inserting contents into table cells

for i in range(0,14):

row=table.rows[i]

row.cells[0].text=reqdheaderList[i]

row.cells[0].paragraphs[0].add_run(line[0]).bold=True

row.cells[1].text=reqdvalueList[i]

救命?

解决方法:

您可以使用以下循环来实现:

bolding_columns = [0]

for row in list(range(14)):

for column in bolding_columns:

table.rows[row].cells[column].paragraphs[0].runs[0].font.bold = True

标签:python-docx,docx,python

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值