Set paragraph font in python-docx

MavWolverine picture MavWolverine · Jan 11, 2015 · Viewed 34.2k times · Source

I am using python-docx 0.7.6.

I can't seem to be able to figure out how to set font family and size for a certain paragraph.

There is .style property but style="Times New Roman" doesn't work.

Can somebody please point me to an example?

Thanks.

Answer

Zenadix picture Zenadix · Jun 5, 2015

This is how to set the Normal style to font Arial and size 10pt.

from docx.shared import Pt

style = document.styles['Normal']
font = style.font
font.name = 'Arial'
font.size = Pt(10)

And this is how to apply it to a paragraph.

paragraph.style = document.styles['Normal']

Using the current version of python-docx (0.8.5).