17

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.

Zenadix
  • 15,291
  • 4
  • 26
  • 41
MavWolverine
  • 846
  • 1
  • 9
  • 24

5 Answers5

45

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

Zenadix
  • 15,291
  • 4
  • 26
  • 41
17

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
Alex Nies
  • 191
  • 1
  • 6
3

Support for run styles has been added in latest version of python-docx

MavWolverine
  • 846
  • 1
  • 9
  • 24
3

Use this code it will help u a lot.

import docx
from docx.shared import Pt
from docx.enum.style import WD_STYLE_TYPE

doc = docx.Document()

parag = doc.add_paragraph("Hello!")

font_styles = doc.styles
font_charstyle = font_styles.add_style('CommentsStyle', WD_STYLE_TYPE.CHARACTER)
font_object = font_charstyle.font
font_object.size = Pt(20)
font_object.name = 'Times New Roman'

parag.add_run("this word document, was created using Times New Roman", style='CommentsStyle').bold = True
parag.add_run("Python", style='CommentsStyle').italic = True
doc.save("test.docx")
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • 1
    Welcome to SO. Please add the relevant information with your code. Please read https://stackoverflow.com/help/how-to-answer – Muhammad Tariq May 01 '21 at 08:58
0

The documentation for python-docx is here: http://python-docx.readthedocs.org/en/latest/

The styles that are available in the default template are listed here: http://python-docx.readthedocs.org/en/latest/user/styles.html

In your example above you used a typeface name ("Times New Roman") instead of a style id. 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. In the meantime, you can define styles for the paragraph and character settings you want and apply those styles. Using styles is the recommended way to apply formatting in Word, similar to how CSS is the recommended way to apply formatting to HTML.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • "In the meantime, you can define styles for the paragraph and character settings you want and apply those styles." Any example of tutorial for this? This is possible with the API right? – MavWolverine Jan 11 '15 at 15:37
  • Using styles might be recommended way. But lets say I have 10 paragraphs with each some slight style change. It doesn't really make sense creating 10 styles to apply them. In word, you can always select a paragraph and apply some style changes only to that paragraph. – MavWolverine Jan 11 '15 at 15:40
  • We are migrating from PHP (PHPDocx) to Python. PHPDocx is way more advanced, hence the expectations. – MavWolverine Jan 11 '15 at 15:41
  • "In the meantime, you can define styles for the paragraph and character settings you want and apply those styles." Any example of tutorial for this? This is possible with the API right? "From the documentation doesn't seem like it." – MavWolverine Jan 11 '15 at 15:43
  • If you're just changing formatting at the in-line level, like a phrase in a different font or something, you can do that with character styles. Those are applied at the run level. If you really do have that many different paragraph styles I'd be interested to hear more about your use case. It would be unusual in my experience to have ten out of paragraphs each having unique paragraph formatting, but I'm always open to new experiences :) – scanny Jan 12 '15 at 07:56
  • Defining styles is not yet possible via the API. It's coming in the next release though, which should be out in a month or so. The way you do it currently is to define those styles in a "template" document and then start with that document, like `document = Document('my-template.docx')`. The documentation has more on that on this page: http://python-docx.readthedocs.org/en/latest/user/documents.html – scanny Jan 12 '15 at 08:00
  • Hi S, in short: users enter some formatted text using TinyMCE which is stored in HTML format. I am writing HTMLToML function. Now since the user can format the text the way they want, I don't have the knowledge of font info beforehand. Hence the problem with styles. – MavWolverine Jan 13 '15 at 15:08