Python: Adjust the Page Size of a Word Document

Page size refers to the dimensions of a document's page. It determines the width and height of the printable area and plays a crucial role in the overall layout and design of the document. Different types of documents may require specific page sizes, such as standard letter size (8.5 x 11 inches) for business letters or A4 size (210 x 297 mm) for international correspondence. Adjusting the page size ensures that your document is compatible with the intended output or presentation medium. In this article, we will demonstrate how to adjust the page size of a Word document in Python using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Adjust the Page Size of a Word Document to a Standard Page Size in Python

With Spire.Doc for Python, you can easily adjust the page sizes of Word documents to a variety of standard page sizes, such as A3, A4, A5, A6, B4, B5, B6, letter, legal, and tabloid. The following steps explain how to change the page size of a Word document to a standard page size using Spire.Doc for Python:

  • Create an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile() method.
  • Iterate through the sections in the document.
  • Set the page size of each section to a standard page size, such as A4, by setting the Section.PageSetup.PageSize property to PageSize.A4().
  • Save the result document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Input.docx")

# Iterate through the sections in the document
for i in range(doc.Sections.Count):
    section = doc.Sections.get_Item(i)
    # Change the page size of each section to A4
    section.PageSetup.PageSize = PageSize.A4()

# Save the result document
doc.SaveToFile("StandardSize.docx", FileFormat.Docx2016)
doc.Close()

Python: Adjust the Page Size of a Word Document

Adjust the Page Size of a Word Document to a Custom Page Size in Python

If you plan to print your document on paper with dimensions that don't match any standard paper size, you can change the page size of your document to a custom page size that matches the exact dimensions of the paper. The following steps explain how to change the page size of a Word document to a custom page size using Spire.Doc for Python:

  • Create an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile() method.
  • Create an instance of the SizeF class with customized dimensions.
  • Iterate through the sections in the document.
  • Set the page size of each section to a custom page size by assigning the SizeF instance to the Section.PageSetup.PageSize property.
  • Save the result document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Input.docx")

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

# Iterate through the sections in the document
for i in range(doc.Sections.Count):
    section = doc.Sections.get_Item(i)
    # Change the page size of each section to the specified dimensions
    section.PageSetup.PageSize = customSize

# Save the result document
doc.SaveToFile("CustomSize.docx", FileFormat.Docx2016)
doc.Close()

Python: Adjust the Page Size of a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.