如何在python中使用docx将此段落加粗
我需要把这一段加粗,但我不能这样做 我使用p.bold=True,但它不起作用 我还为每一行使用了add_run,但它显示int类型和bool类型没有任何属性add_run如何在python中使用docx将此段落加粗,python,python-docx,Python,Python Docx,我需要把这一段加粗,但我不能这样做 我使用p.bold=True,但它不起作用 我还为每一行使用了add_run,但它显示int类型和bool类型没有任何属性add_run p = document.add_paragraph(F""" {doj} Employee Code: {record[57]} {record[1]} {record[2]} {record[3]} {record[4]} {record[12]}, {record[13]},{record[15]
p = document.add_paragraph(F"""
{doj}
Employee Code: {record[57]}
{record[1]} {record[2]} {record[3]} {record[4]}
{record[12]},
{record[13]},{record[15]}
Dear {record[2]},""")
我试着这样做,但没有成功
document = Document()
p = document.add_paragraph(F"""
{doj}""").bold = True
p.add_run(F"""Employee Code: {record[57]}""").bold = True
p.add_run(F"""{record[1]} {record[2]} {record[3]} {record[4]}""").bold = True
p.add_run(F"""{record[12]},""").bold = True
p.add_run(F"""{record[13]},{record[15]}""").bold = True
p.add_run(F"""Dear {record[2]},""").bold = True
希望这能解决你的问题 解释提供于
您使用的python版本是什么?OP似乎正确地使用了.bold语法。我看不出这个答案是如何解决这个问题的。我本来想把它作为一个评论加上去的。
Run objects have both a .bold and .italic property that allows you to set their value for a run:
paragraph = document.add_paragraph('Lorem ipsum ')
run = paragraph.add_run('dolor')
run.bold = True
paragraph.add_run(' sit amet.')
which produces text that looks like this: ‘Lorem ipsum dolor sit amet.’
Note that you can set bold or italic right on the result of .add_run() if you don’t need it for anything else:
paragraph.add_run('dolor').bold = True
# is equivalent to:
run = paragraph.add_run('dolor')
run.bold = True
# except you don't have a reference to `run` afterward
It’s not necessary to provide text to the .add_paragraph() method. This can make your code simpler if you’re building the paragraph up from runs anyway:
paragraph = document.add_paragraph()
paragraph.add_run('Lorem ipsum ')
paragraph.add_run('dolor').bold = True
paragraph.add_run(' sit amet.')