加粗表格单元格
我有以下代码:
table = document.add_table(rows=1, cols=8)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Surname'
hdr_cells[2].text = 'Telephone'
(...)
有没有办法让这些单元格变成粗体呢?像这样:
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Surname'
hdr_cells[2].text = 'Telephone'
hdr_cells[0].text.bold = True
hdr_cells[1].text.bold = True
hdr_cells[2].text.bold = True
1 个回答
8
在这个特定的情况下,这样做应该就能解决问题:
hdr_cells[0].paragraphs[0].add_run('Name').bold = True
或者,更详细一点的步骤:
col_names = ('Name', 'Surname', 'Telephone')
table = document.add_table(rows=1, cols=len(col_names))
hdr_cells = table.rows[0].cells
for idx, name in enumerate(col_names):
paragraph = hdr_cells[idx].paragraphs[0]
run = paragraph.add_run(name)
run.bold = True