Search code examples
python-3.xpython-docx

How to configure header's (and footer's) "Header from Top" value when using Python's docx library?


I am trying to create from scratch a docx file report, so I am using the python-docx library ver 0.8.10, with great results. When I am adding a header to the document, the header has the default values of "Header from Top:" = 0.5 Inches. I see this value when I have created the docx file and opened it in MS WORD.

So, how can I configure the "Header from Top" and the "Footer from Bottom" default values? For example How can I configure the header to be "Header from Top" = 0.1 inches? Something like header.top_margins = Inches(0.1). I have checked online but haven't found anything related.

Thanks in advance


Solution

  • I think you're looking for Section.header_distance and .footer_distance:
    https://python-docx.readthedocs.io/en/latest/api/section.html#docx.section.Section.header_distance

    header_distance
    Length object representing the distance from the top edge of the page to the top edge of the header. None if no setting is present in the XML.