Search code examples
pythondocxpython-docx

How do I apply both bold and center in python-docx?


I'm using python-docx to put a text into MS Word. I can make it bold or center,but how to do both.
Here's the bold:

p=document.add_paragraph().add_run('test word')
p.font.size = Pt(16)
p.bold = True

Here's the center:

p=document.add_paragraph('test word')
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

how to do both bold and center?


Solution

  • Separate between paragraph and run and define each:

    p=document.add_paragraph()
    p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    r=p.add_run('test word')
    r.font.size = Pt(16)
    r.bold = True