繁体   English   中英

我如何在 python 中使用 docx 使这一段加粗

[英]How i do make this paragraph bold using docx in python

我需要将这一段加粗,但我无法这样做我使用了 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]}

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-docx 中同时应用粗体和斜体?

https://python-docx.readthedocs.io/en/latest/user/quickstart.html提供的说明


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM