How to Add Page Numbers in Word Documents: Python Solution
Although page numbers are rarely a hot topic when discussing Word documents, they play a crucial role, especially in lengthy documents. Page numbers not only help readers quickly navigate to specific sections but also make printed versions more organized. In this article, we will show you how to add page numbers in Word using Python, offering a more efficient and flexible way to manage your documents. Let's dive in!
Python Library to Add Page Numbers in Word Documents
In this guide, to better accomplish the task, we will use Spire.Doc for Python to demonstrate page numbering in Word documents. This robust Word library supports a wide range of Microsoft Word features, allowing developers to insert and manage page numbers without hassle.
You can install it using the pip command: pip install Spire.Doc
.
How to Add Page Numbers in Word: Each Page
After the preparation, let’s get down to the main topic. When talking about adding page numbers in Word documents, the most common requirement is to add page numbers to every page. This not only provides a quick overview of the total number of pages in a Word document but also makes it easier to locate specific information efficiently.
Steps to insert page numbers in Word documents:
Create an object of the Document class and use the Document.LoadFromFile() method to read a Word document.
Get a section with the Document.Sections[] property, and then get the footer of the section using the Section.HeadersFooters.Footer property.
Add a new paragraph in the footer area by calling the Footer.AddParagraph() method.
Add page numbers to the footer with the Footer.AddParagraph.AppendField() and Footer.AddParagraph.AppendText() methods.
Customize the appearance of the page numbers by accessing properties under the CharacterFormat class.
Save the document as a new Word file using the Document.SaveToFile() method.
Here is an example of adding “page number/page count” to each page of a Word document:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("Input.docx")
# Get the first section
section = document.Sections[0]
# Get the footer of the section
footer = section.HeadersFooters.Footer
# Add "page number/page count" to the footer
footerParagraph = footer.AddParagraph()
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(" / ")
footerParagraph.AppendField("page count", FieldType.FieldNumPages)
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# Apply formatting to the page number
style = ParagraphStyle(document)
style.CharacterFormat.Bold = True
style.CharacterFormat.FontName = "Times New Roman"
style.CharacterFormat.FontSize = 18
style.CharacterFormat.TextColor = Color.get_Red()
document.Styles.Add(style)
footerParagraph.ApplyStyle(style)
# Save the document
document.SaveToFile("AddPageNumbersToDocument.docx")
# Dispose resources
document.Dispose()
How to Add Page Numbers in Word: to a Specified Section
To insert page numbers to a Specified Section in Word documents, you can use the AppendField() and AppendText() methods as well. However, this process differs significantly from the steps in the previous section. Typically, all page numbers in a Word document are interconnected. To assign a unique page number to a specific section without altering others, you need to unlink the page numbers using the HeadersFooters.Footer.LinkToPrevious property. Now, let’s take a closer look at how it works.
Steps to insert page numbers to a certain section in Word:
Create a Document instance and open a Word document with the Document.LoadFromFile() method.
Get a section using the Document.Sections[] property, and then get the footer of the section with the Section.HeadersFooters.Footer property.
Restart page numbering from 1 in the section by setting the Section.PageSetup.RestartPageNumbering property to
True
, and the Section.PageSetup.PageStartingNumber property to1
.Add a new paragraph in the footer area by calling the Footer.AddParagraph() method.
Add page numbers to the footer with the Footer.AddParagraph.AppendField() and Footer.AddParagraph.AppendText() methods.
Customize the appearance of the page numbers by accessing properties under the CharacterFormat class.
Cut the link of page numbers by setting the HeadersFooters.Footer.LinkToPrevious property to False.
Delete the content of the footers in the subsequent sections.
Save the newly updated Word file.
Below is the code example of inserting page numbers to the second section:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("Input.docx")
# Get a specific section
sectionIndex = 1
section = document.Sections[sectionIndex]
# Restart page numbering from 1
section.PageSetup.RestartPageNumbering = True
section.PageSetup.PageStartingNumber = 1
# Get the footer of the section
footer = section.HeadersFooters.Footer
# Add "Page X, Section Y" to the footer
footerParagraph = footer.AddParagraph()
footerParagraph.AppendText("Page ")
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(", Section ")
footerParagraph.AppendField("section number", FieldType.FieldSection)
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# Apply formatting to the page number
style = ParagraphStyle(document)
style.CharacterFormat.Bold = True
style.CharacterFormat.FontName = "Times New Roman"
style.CharacterFormat.FontSize = 18
style.CharacterFormat.TextColor = Color.get_Red()
document.Styles.Add(style)
footerParagraph.ApplyStyle(style)
# Disable "Link to previous" in the subsequent section
document.Sections[sectionIndex + 1].HeadersFooters.Footer.LinkToPrevious = False
# Delete the content of the footers in the subsequent sections
for i in range(sectionIndex + 1, document.Sections.Count):
document.Sections[i].HeadersFooters.Footer.ChildObjects.Clear()
document.Sections[i].HeadersFooters.Footer.AddParagraph()
# Save the document
document.SaveToFile("AddPageNumbersToSection.docx")
# Dispose resources
document.Dispose()
Insert Page Numbers in Word: Discontinuous Page Numbers in Different Sections
For Word documents with multiple sections, you may need to assign separate page numbers to each section, such as starting from the first page of each section. This approach helps distinguish content between sections. To achieve this, iterate through each section, add the page number, and unlink the page numbers to avoid interference. While it may seem complex, the steps and code examples below simplify the process.
Steps to add page numbers in different sections:
Create an instance of the Document class and specify the file path to load a Word document.
Iterate through all sections.
Get a section with the Document.Sections[] property, and then get the footer of the section with the Section.HeadersFooters.Footer property.
Set the page numbering start from 1 for each section by setting the Section.PageSetup.RestartPageNumbering property to True, and the Section.PageSetup.PageStartingNumber property to 1.
Insert page numbers to the footer with the Footer.AddParagraph.AppendField() and Footer.AddParagraph.AppendText() methods.
Save the modified Word document to the local storage.
Here is the code example of adding discontinuous page numbers to different sections:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("Input.docx")
# Iterate through the sections in the document
for i in range(document.Sections.Count):
# Get a specific section
section = document.Sections[i]
# Restart page numbering from 1
section.PageSetup.RestartPageNumbering = True
section.PageSetup.PageStartingNumber = 1
# Get the footer of the section
footer = section.HeadersFooters.Footer
# Add "Page X, Section Y" to the footer
footerParagraph = footer.AddParagraph()
footerParagraph.AppendText("Page ")
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(", Section ")
footerParagraph.AppendField("section number", FieldType.FieldSection)
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# Apply formatting to the page number
style = ParagraphStyle(document)
style.CharacterFormat.Bold = True
style.CharacterFormat.FontName = "Times New Roman"
style.CharacterFormat.FontSize = 18
style.CharacterFormat.TextColor = Color.get_Red()
document.Styles.Add(style)
footerParagraph.ApplyStyle(style)
# Save the document
document.SaveToFile("AddDifferentPageNumbersToSections.docx")
# Dispose resources
document.Dispose()
The conclusion
This article explains how to add page numbers to a Word document with Python, including consecutive numbering for all pages, starting page numbers from a specific page, and assigning independent page numbers to different sections. To enhance understanding, code examples are provided. We hope you find this guide helpful!
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by