Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set paragraph font in python-docx

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.

like image 669
MavWolverine Avatar asked Jan 11 '15 06:01

MavWolverine


People also ask

How do I apply a font to a paragraph in Python docx?

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. Using the current version of python-docx (0.8.5). After reading through the API documentation I was able to figure out how to create my own style and apply it.

How do I add a paragraph to a block in Python?

You can directly use add_paragraph () method to add paragraph but if you want to apply a font colour to a text you have to use add_run () as all the block-level formatting is done by using add_paragraph () method while all the character-level formatting is done by using add_run ().

How to apply a style to a paragraph in Python-docx?

And this is how to apply it to a paragraph. Using the current version of python-docx (0.8.5). After reading through the API documentation I was able to figure out how to create my own style and apply it. You could create a paragraph style object the same way by changing this code to use WD_STYLE_TYPE.PARAGRAPH.

Can I change the font size of text in Python-docx?

If you use "Heading1" for example, that would change the look of the font, among other things, since it's a paragraph style. At present there is no API for directly applying a typeface name or font size to text in python-docx, although that and more is coming in the next release, probably within a month.


2 Answers

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

like image 179
Zenadix Avatar answered Oct 16 '22 08:10

Zenadix


After reading through the API documentation I was able to figure out how to create my own style and apply it. You could create a paragraph style object the same way by changing this code to use WD_STYLE_TYPE.PARAGRAPH. Something that took me a minute to figure out was the objects and at what level they are applied so just make sure you understand that clearly. Something I found counter intuitive is that you define the styles properties after its creation.

This is how I created a character level style object.

document = Document(path to word document)

# Creates a character level style Object ("CommentsStyle") then defines its parameters

obj_styles = document.styles
obj_charstyle = obj_styles.add_style('CommentsStyle', WD_STYLE_TYPE.CHARACTER)
obj_font = obj_charstyle.font
obj_font.size = Pt(10)
obj_font.name = 'Times New Roman'

This is how I applied the style to a run.

paragraph.add_run(any string variable, style = 'CommentsStyle').bold = True
like image 15
Alex Nies Avatar answered Oct 16 '22 10:10

Alex Nies