Add and Customize Headers and Footers in Word Documents with Python

Alice Yang
6 min readAug 30, 2023

--

Headers and footers are essential elements of a professional-looking document. They provide a consistent visual identity, assist in document navigation, and offer a place to add important information such as page numbers, dates, author names, and company logos. In this article, we will explore how to add headers and footers to Word documents using Python.

We will discuss the following topics:

Python Library to Operate Headers and Footers in Word Documents

To work with headers and footers in Word documents with Python, we can use the Spire.Doc for Python library.

Spire.Doc for Python is a feature-rich and easy-to-use library for creating, reading, editing, and converting Word files within Python applications. With this library, you can work with a wide range of Word formats, including Doc, Docx, Docm, Dot, Dotx, Dotm, and more. Moreover, you are also able to render Word documents to other types of file formats, such as PDF, RTF, HTML, Text, Image, SVG, ODT, PostScript, PCL, and XPS.

You can install Spire.Doc for Python from pypi by running the following command in your terminal:

pip install Spire.Doc

For more detailed information about the installation, you can check this official documentation: How to Install Spire.Doc for Python in VS Code.

Add Header and Footer to Word using Python

Adding a header and footer to a Word document allows you to include information that will appear at the top and bottom of every page. This information can include text such as page numbers, document titles, author names, dates, and/or images like company logos.

The following code snippet shows how to add a header with text and image and a footer with text (page number) to a Word document with Python:

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the first section
section = document.Sections[0]

# Get the header of the section
pageHeader = section.HeadersFooters.Header

# Add a paragraph to the header
headerPara = pageHeader.AddParagraph()

# Add text to the header paragraph
text = headerPara.AppendText("HOME-BASED BUSINESS")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 12
text.CharacterFormat.Italic = True
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Right

# Add an image to the header paragraph
headerPicture = headerPara.AppendPicture("logo.png")

# Set text wrapping style of the header image
headerPicture.TextWrappingStyle = TextWrappingStyle.Behind

# Set position of the header image
headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
headerPicture.VerticalOrigin = VerticalOrigin.TopMarginArea
headerPicture.VerticalAlignment = ShapeVerticalAlignment.Center

# Set the bottom border style for the header
headerPara.Format.Borders.Bottom.BorderType = BorderStyle.Single
headerPara.Format.Borders.Bottom.Space = 0.05

# Get the footer of the section
pageFooter = section.HeadersFooters.Footer

# Add a paragraph to the footer
footerPara= pageFooter.AddParagraph()

# Add the current page number and the total page number to the footer paragraph
footerPara.AppendField("page number", FieldType.FieldPage)
footerPara.AppendText(" of ")
footerPara.AppendField("number of pages", FieldType.FieldNumPages)
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Right

# Set the top border style for the footer
footerPara.Format.Borders.Top.BorderType = BorderStyle.Single
footerPara.Format.Borders.Top.Space = 0.05

# Save the resulting document
document.SaveToFile("HeaderAndFooter.docx", FileFormat.Docx2016)
document.Close()
Add Header and Footer to Word in Python
Add Header and Footer to Word in Python

Add Header and Footer for the First Page Only in Word using Python

In some cases, you may only want to add a header or footer for the first page of a Word document. This can be useful when creating a cover page with unique information.

The following code snippet shows how to add a header and footer for only the first page of a Word document with Python:

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the first section
section = document.Sections[0]
# Enable the DifferentFirstPageHeaderFooter property
section.PageSetup.DifferentFirstPageHeaderFooter = True

# Get the first page header
firstPageHeader = section.HeadersFooters.FirstPageHeader
# Add a paragraph to the header
firstPageHeaderPara = firstPageHeader.AddParagraph()

# Add text to the header paragraph
text = firstPageHeaderPara.AppendText("No. 2023011023758-SOW #001")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
text.CharacterFormat.TextColor = Color.get_Blue()
text.CharacterFormat.Italic = True
firstPageHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Right

# Set the bottom border style for the header
firstPageHeaderPara.Format.Borders.Bottom.BorderType = BorderStyle.Single
firstPageHeaderPara.Format.Borders.Bottom.Space = 0.05

# Get the first page footer
firstPageFooter = section.HeadersFooters.FirstPageFooter
# Add a paragraph to the footer
firstPageFooterPara = firstPageFooter.AddParagraph()

# Add text to the footer paragraph
text = firstPageFooterPara.AppendText("HOME-BASED BUSINESS")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
text.CharacterFormat.TextColor = Color.get_Blue()
firstPageFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Set the top border style for the footer
firstPageFooterPara.Format.Borders.Top.BorderType = BorderStyle.Single
firstPageFooterPara.Format.Borders.Top.Space = 0.05

#Save the resulting document
document.SaveToFile("HeaderAndFooterForFirstPageOnly.docx", FileFormat.Docx2016)
document.Close()
Add Header and Footer for the First Page Only in Word in Python
Add Header and Footer for the First Page Only in Word in Python

Add Different Headers and Footers for Odd and Even Pages in Word using Python

When working on a document that requires different formatting for odd and even pages, such as in a book or report, you can add different headers and footers for each page type.

The following code snippet shows how to add different headers and footers for odd and even pages of a Word document with Python:

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the first section
section = document.Sections[0]
# Enable the DifferentOddAndEvenPagesHeaderFooter property
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = True

# Add odd page header
oddPageHeader = section.HeadersFooters.OddHeader
oddPageHeaderPara = oddPageHeader.AddParagraph()
text = oddPageHeaderPara.AppendText("Odd Header")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
oddPageHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add odd page footer
oddPageFooter = section.HeadersFooters.OddFooter
oddPageFooterPara = oddPageFooter.AddParagraph()
text = oddPageFooterPara.AppendText("Odd Footer")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
oddPageFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add even page header
evenPageHeader = section.HeadersFooters.EvenHeader
evenPageHeaderPara = evenPageHeader.AddParagraph()
text = evenPageHeaderPara.AppendText("Even Header")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
evenPageHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add even page footer
evenPageFooter = section.HeadersFooters.EvenFooter
evenPageFooterPara = evenPageFooter.AddParagraph()
text = evenPageFooterPara.AppendText("Even Footer")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
evenPageFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Save the resulting document
document.SaveToFile("DifferentHeadersAndFootersForOddAndEvenPages.docx", FileFormat.Docx2016)
document.Close()
Add Different Headers and Footers for Odd and Even Pages in Word in Python
Add Different Headers and Footers for Odd and Even Pages in Word in Python

Add Different Header and Footer for the First Page in Word using Python

If you want to differentiate the first page from the rest of the document, you can add a different header and footer for the first page and the same header and footer for the other pages.

The following code snippet shows how to add a different header and footer for the first page and the same header and footer for the other pages of a Word document with Python:

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the first section
section = document.Sections[0]
# Enable the DifferentFirstPageHeaderFooter property
section.PageSetup.DifferentFirstPageHeaderFooter = True

# Add first page header
firstPageHeader = section.HeadersFooters.FirstPageHeader
firstPageHeaderPara = firstPageHeader.AddParagraph()
text = firstPageHeaderPara.AppendText("First Page Header")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
firstPageHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add first page footer
firstPageFooter = section.HeadersFooters.FirstPageFooter
firstPageFooterPara = firstPageFooter.AddParagraph()
text = firstPageFooterPara.AppendText("First Page Header")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
firstPageFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add other page header
pageHeader = section.HeadersFooters.Header
pageHeaderPara = pageHeader.AddParagraph()
text = pageHeaderPara.AppendText("Other Page Header")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
pageHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add other page footer
pageFooter = section.HeadersFooters.Footer
pageFooterPara = pageFooter.AddParagraph()
text = pageFooterPara.AppendText("Other Page Footer")
text.CharacterFormat.FontName = "Calibri"
text.CharacterFormat.FontSize = 14
pageFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center

# Save the resulting document
document.SaveToFile("DifferentHeadersAndFootersForFirstPage.docx", FileFormat.Docx2016)
document.Close()
Add Different Header and Footer for the First Page in Word in Python
Add Different Header and Footer for the First Page in Word in Python

Conclusion

This article demonstrated various scenarios to add headers and footers to Word documents using Python and Spire.Doc for Python. We hope you can find it helpful.

Related Topics

Convert Word DOC or DOCX to PDF with Python: A Comprehensive Guide

Convert Word Documents to Images (PNG, JPG, BMP) with Python

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.