Change the Page Size and Margins of a Word Document with Python

Alexander Stock
4 min readJan 12, 2024

--

Change page size and margins of a Word document.

Changing the page size and margins of a Word document allows you to customize the layout and formatting to meet your specific needs. By adjusting the page size, you can ensure your content fits well on the page, while modifying the margins allows you to control the spacing around the text.

In this bolg post, I am going to introduce how to change the page size and margins of a Word document in Python using Spire.Doc for Python.

Install Dependency

This solution requires Spire.Doc for Python to be installed as the dependency, which is a Word library for reading, creating and manipulating Word documents in a Python program. You can install it by running the following pip command.

pip install Spire.Doc

Background Knowledge

As you may know, a Word document consists of a least one section. A section refers to a distinct portion or division of the document that can have its own formatting properties. Sections in a Word document enable you to control page size, headers and footers, margins, orientation, page numbering, and other formatting settings.

Spire.Doc for Python offers the Document.Sections.get_Item() method that you can use to get a specific section from a document. To change the page size or the margins of the section, you can use the Section.PageSetup.PageSize property and the Section.PageSetup.Margins property. To keep the document looking neat and consistent, we usually set the page size and margins of each section to be the same.

Change the Page Size of a Word to a Standard Size with Python

In Microsoft Word, you can choose from a variety of predefined page sizes to suit your needs. These standard options include Latter, Legal, A4, A5, B5, Executive, Tabloid and Statement. Spire.Doc provides the method PageSize.Latter(), PageSize.Legal(), PageSize.A4(), etc. for users to easily obtain a standard page size, which can be applied to Section.PageSetup.PageSize object.

The following code snippet demonstrates how to change the page size of a Word document to Legal in Python.

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

# Create a Document object
doc = Document()

# Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Iterate through the sections in the document
for i in range(doc.Sections.Count):

# Get a specific section
section = doc.Sections.get_Item(i)

# Change the page size to Legal
section.PageSetup.PageSize = PageSize.Legal()

# Save the changes to file
doc.SaveToFile("output/StandardSize.docx", FileFormat.Docx2019)
doc.Close()

Change the Page Size of a Word to a Custom Size with Python

Word allows you to set custom page sizes if you have specific requirements beyond the standard options. To do it programmatically, create a SizeF object with customized dimensions and apply it to a section through Section.PageSetup.PageSize property.

This example shows how to customize the page size of a Word document in Python.

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

# Create a Document object
doc = Document()

# Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Create an instance of the SizeF class with customized dimensions
customSize = SizeF(550.0, 800.0)

# Iterate through the sections in the document
for i in range(doc.Sections.Count):

# Get a specific section
section = doc.Sections.get_Item(i)

# Change the page size to the custom size
section.PageSetup.PageSize = customSize

# Save the changes to file
doc.SaveToFile("Output/CustomSize.docx", FileFormat.Docx2019)
doc.Close()

Change the Margins of a Word with Python

As mentioned earlier, Section.PageSetup.Margins is used to get the margins of a page. Specifically, you can change the left, top, right and bottom margin through the property Margins.Left, Margins.Top, Margins.Right, Margins.Bottom, respectively. To change them all at once, use the Margins.All property.

Here is an example illustrating how to change the margins of a Word document in Python.

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

# Create a Document object
doc = Document()

# Load a Word document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Loop through the sections of document
for i in range(doc.Sections.Count):

# Get a specific section
section = doc.Sections.get_Item(i)

# Get the margins of the section
margins = section.PageSetup.Margins

# Set the values (in point) of top, bottom, left, right margin
margins.Top = 50.0
margins.Bottom = 50.0
margins.Left = 45.0
margins.Right = 45.0
# margins.All = 50.0

# Save the document
doc.SaveToFile("output/SetPageMargins.docx", FileFormat.Docx2019)
doc.Close()

Conclusion

By using the code snippets outlined in this blog post, you can effortlessly switch between standard page sizes and customize dimensions, and customize the page margins of a Word document as well.

Related Topics

Convert Word to PDF in Python

Convert Word to Images in Python

Add or Remove Wartermarks in Word in Python

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.