Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly indent with python-docx?

Indentation seems pretty straightforward, and terminal prints back the proper indentation, but the same indentation is not reflected in my saved Word docx.
Am I doing something wrong here?

from docx import Document
from docx.shared import Inches
    
worddoc = Document()
paragraph = worddoc.add_paragraph('Left Indent Test')
paragraph.left_indent = Inches(.25)
print(paragraph.left_indent.inches)
    
worddoc.save('left_indent.docx')
like image 247
j4w Avatar asked Sep 29 '25 09:09

j4w


1 Answers

This turns out to be a documentation error.

If you use the new API it works:

paragraph.paragraph_format.left_indent = Inches(0.25)

The left_indent property was moved to the paragraph_format "sub-object" a couple releases back as the ParagraphFormat class is used by both the Paragraph and ParagraphStyle objects.

If you'll file a bug report in the python-docx issue tracker on GitHub we'll get the documentation updated next time we're in there.

like image 118
scanny Avatar answered Oct 02 '25 04:10

scanny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!