Last modified: Nov 08, 2025 By Alexander Williams

Python-docx Headers Footers Guide

Headers and footers are essential in professional documents. They contain important information like page numbers and titles. Python-docx makes adding them easy.

This guide covers everything about headers and footers. You will learn to add text, images, and page numbers. We include practical code examples for each step.

Understanding Headers and Footers

Headers appear at the top of each page. Footers appear at the bottom. They are separate from the main document content.

Python-docx provides special sections for headers and footers. Each section can have different content. You can customize them as needed.

If you are new to python-docx, check our Python-docx Tutorial: Create Word Documents. It covers the basics you need to know.

Accessing Headers and Footers

First, you need to access the document sections. Each section has header and footer properties. Use sections to get all sections.


from docx import Document

# Create a new document
doc = Document()

# Access the first section
section = doc.sections[0]

# Access header and footer
header = section.header
footer = section.footer

This code gets the header and footer of the first section. Most documents have only one section. You can add more if needed.

Adding Text to Headers

Headers can contain text, like document titles. Use header.paragraphs[0] to access the first paragraph. Then add text to it.


# Add text to header
header_paragraph = header.paragraphs[0]
header_paragraph.text = "My Document Header"

This adds "My Document Header" to the header. The text will appear on every page. You can format it like any other paragraph.

For detailed formatting options, see our Python-docx Paragraph Formatting Guide. It explains how to change fonts and styles.

Adding Text to Footers

Footers work the same way as headers. Use footer.paragraphs[0] to access the footer paragraph. Then add your text.


# Add text to footer
footer_paragraph = footer.paragraphs[0]
footer_paragraph.text = "Confidential Document"

This adds "Confidential Document" to the footer. It will appear at the bottom of every page. You can align it left, center, or right.

Adding Page Numbers

Page numbers are common in footers. Python-docx does not have built-in page numbers. But you can add them as fields.

Use add_run to add a page number field. The field code is PAGE. Word will replace it with the actual page number.


# Add page number to footer
footer_paragraph = footer.paragraphs[0]
footer_paragraph.text = "Page "
run = footer_paragraph.add_run()
run.text = "1"  # Placeholder, Word will update it

This adds "Page 1" to the footer. Word will update the number on each page. It is a simple but effective method.

Different First Page Headers

Some documents need different first page headers. Python-docx supports this feature. Set different_first_page_header_footer to True.


# Enable different first page
section.different_first_page_header_footer = True

# Access first page header
first_page_header = section.first_page_header
first_page_header.paragraphs[0].text = "First Page Header"

This sets a special header for the first page. The rest of the pages will use the regular header. It is useful for cover pages.

Adding Images to Headers

You can add images to headers and footers. Use the add_picture method. It works the same as in the main document.


# Add image to header
header_paragraph = header.paragraphs[0]
run = header_paragraph.add_run()
run.add_picture("logo.png")

This adds an image named "logo.png" to the header. The image will appear on every page. Make sure the file path is correct.

For more on images, see Add Images to docx Using Python-docx. It covers sizing and positioning.

Complete Example

Here is a complete example. It creates a document with a header and footer. The header has text, and the footer has a page number.


from docx import Document

# Create document
doc = Document()

# Access section
section = doc.sections[0]

# Set header
header = section.header
header_paragraph = header.paragraphs[0]
header_paragraph.text = "Annual Report 2024"

# Set footer with page number
footer = section.footer
footer_paragraph = footer.paragraphs[0]
footer_paragraph.text = "Page "
run = footer_paragraph.add_run()
run.text = "1"

# Add some content to the document
doc.add_paragraph("This is the main content of the document.")

# Save the document
doc.save("document_with_header_footer.docx")

This code creates a professional document. The header shows the title. The footer shows the page number.

Conclusion

Headers and footers are vital for professional documents. Python-docx makes them easy to manage. You can add text, images, and page numbers.

Remember to access the section first. Then modify the header and footer properties. Use different first page headers when needed.

Mastering headers and footers will improve your document automation skills. Combine this with our other guides for full control over Word documents.