Add page number using python docx

When working with Python and the docx library, it is common to come across the need to add page numbers to a document. In this article, we will explore three different ways to achieve this task.

Option 1: Using python-docx

The first option is to use the python-docx library, which provides a convenient way to manipulate Word documents. To add page numbers using this library, we can follow these steps:

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt

def add_page_numbers(document):
    sections = document.sections
    for section in sections:
        footer = section.footer
        footer_paragraph = footer.paragraphs[0]
        footer_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
        page_number_run = footer_paragraph.add_run()
        page_number_run.text = f"Page {section.page_number}"
        page_number_run.font.size = Pt(10)

# Example usage
document = Document("example.docx")
add_page_numbers(document)
document.save("example_with_page_numbers.docx")

This code snippet demonstrates how to add page numbers to a Word document using the python-docx library. It first retrieves the footer of each section in the document and aligns it to the right. Then, it adds a run to the footer paragraph and sets its text to “Page X”, where X is the current page number. Finally, it sets the font size of the page number to 10 points.

Option 2: Using python-docx-template

If you prefer a more template-based approach, you can use the python-docx-template library. This library allows you to define a Word document template with placeholders that can be filled in dynamically. Here’s an example of how to add page numbers using this library:

from docxtpl import DocxTemplate

def add_page_numbers(template_path, output_path):
    doc = DocxTemplate(template_path)
    context = {"page_number": "Page {{ page_number }}"}
    doc.render(context)
    doc.save(output_path)

# Example usage
template_path = "template.docx"
output_path = "output.docx"
add_page_numbers(template_path, output_path)

In this code snippet, we first create a DocxTemplate object from a template document. We then define a context dictionary with a “page_number” key that contains the placeholder for the page number. The placeholder is defined using double curly braces to indicate that it should be replaced with the actual page number. Finally, we render the template with the context and save the output document.

Option 3: Using python-docx2pdf

If you need to convert the Word document to PDF and add page numbers in the process, you can use the python-docx2pdf library. This library allows you to convert Word documents to PDF format and add page numbers automatically. Here’s an example:

from docx2pdf import convert

def add_page_numbers(input_path, output_path):
    convert(input_path, output_path)

# Example usage
input_path = "input.docx"
output_path = "output.pdf"
add_page_numbers(input_path, output_path)

In this code snippet, we simply call the convert function from the python-docx2pdf library, passing the input Word document path and the output PDF document path. The library takes care of converting the document and adding page numbers automatically.

After exploring these three options, it is clear that the best choice depends on the specific requirements of your project. If you need fine-grained control over the page number placement and formatting, option 1 using python-docx is the most suitable. If you prefer a template-based approach, option 2 using python-docx-template is a good choice. Finally, if you need to convert the document to PDF and add page numbers in the process, option 3 using python-docx2pdf is the way to go.

Rate this post

7 Responses

    1. I couldnt disagree more! Templates are a lifesaver when it comes to creating professional and consistent documents. Python-docx might be easy, but it cant match the flexibility and customization options that templates offer. Youll change your mind once you try them!

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents