Last modified: Nov 08, 2025 By Alexander Williams

Python-docx Page Setup: Margins, Orientation, Layout

Page setup is crucial for professional documents. It controls how content appears on the page. Python-docx makes this easy to automate.

This guide covers margins, orientation, and layout. You will learn to create polished documents programmatically.

Understanding Page Setup in Python-docx

Every Word document has page setup properties. These control the overall document appearance. Python-docx accesses these through sections.

A document contains one or more sections. Each section can have its own page setup. This allows different layouts within one document.

The sections property gives access to all sections. You typically work with the first section for simple documents.

Setting Document Margins

Margins define the space around page content. They affect readability and professional appearance. Python-docx lets you set all margin types.

Use the left_margin, right_margin, top_margin, and bottom_margin properties. All values are in inches.

Here is a practical example of setting margins:


from docx import Document
from docx.shared import Inches

# Create a new document
doc = Document()

# Access the first section
section = doc.sections[0]

# Set all margins to 1 inch
section.left_margin = Inches(1)
section.right_margin = Inches(1)
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)

# Add some content
doc.add_paragraph("This document has 1-inch margins on all sides.")

# Save the document
doc.save("margins_document.docx")

This code creates a document with equal margins. The Inches class converts numeric values to the correct format.

Changing Page Orientation

Page orientation determines document layout. Portrait is default for most documents. Landscape works better for wide tables and charts.

Use the orientation property to change orientation. Set it to WD_ORIENTATION.PORTRAIT or WD_ORIENTATION.LANDSCAPE.

Here is how to create a landscape document:


from docx import Document
from docx.enum.section import WD_ORIENTATION
from docx.shared import Inches

# Create document
doc = Document()

# Get first section
section = doc.sections[0]

# Change to landscape orientation
section.orientation = WD_ORIENTATION.LANDSCAPE

# Adjust margins for landscape
section.left_margin = Inches(0.5)
section.right_margin = Inches(0.5)

# Add content
doc.add_paragraph("This is a landscape document.")

# Save
doc.save("landscape_document.docx")

Note that changing orientation may require margin adjustments. Landscape pages often need smaller side margins.

Controlling Page Size and Layout

Page size is another important aspect. The default is usually letter size (8.5x11 inches). You can change this for different paper standards.

Use page_height and page_width properties. Set them using the Inches class for consistency.

Here is an A4 page setup example:


from docx import Document
from docx.shared import Inches

# Create document
doc = Document()

section = doc.sections[0]

# Set A4 page size (8.27 x 11.69 inches)
section.page_height = Inches(11.69)
section.page_width = Inches(8.27)

# Set appropriate margins
section.left_margin = Inches(1)
section.right_margin = Inches(1)

doc.add_paragraph("A4 size document with proper margins.")
doc.save("a4_document.docx")

This creates a standard A4 document. Remember to adjust margins for different page sizes.

Working with Multiple Sections

Complex documents may need different layouts. You can achieve this using multiple sections. Each section maintains its own page setup.

Add new sections with doc.add_section(). Then configure each section independently.

Here is a multi-section document example:


from docx import Document
from docx.enum.section import WD_ORIENTATION
from docx.shared import Inches

doc = Document()

# First section - portrait
section1 = doc.sections[0]
section1.orientation = WD_ORIENTATION.PORTRAIT
section1.left_margin = Inches(1.25)

doc.add_paragraph("First section: Portrait orientation")

# Add new section - landscape
doc.add_section()
section2 = doc.sections[1]
section2.orientation = WD_ORIENTATION.LANDSCAPE
section2.left_margin = Inches(0.5)

doc.add_paragraph("Second section: Landscape orientation")

doc.save("multi_section_document.docx")

This technique is useful for reports with mixed content. You can combine text pages with wide data tables.

Practical Page Setup Example

Let's create a complete business report. It will demonstrate realistic page setup usage.


from docx import Document
from docx.enum.section import WD_ORIENTATION
from docx.shared import Inches

# Create professional report
doc = Document()

# Configure main section
section = doc.sections[0]
section.left_margin = Inches(1)
section.right_margin = Inches(1)
section.top_margin = Inches(0.75)
section.bottom_margin = Inches(0.75)

# Add title
title = doc.add_heading('Quarterly Business Report', 0)
title.alignment = 1  # Center alignment

# Add content paragraphs
doc.add_paragraph("Executive Summary:")
doc.add_paragraph("This report shows strong growth across all metrics...")

# Add landscape section for data
doc.add_section()
landscape_section = doc.sections[1]
landscape_section.orientation = WD_ORIENTATION.LANDSCAPE
landscape_section.left_margin = Inches(0.5)
landscape_section.right_margin = Inches(0.5)

doc.add_paragraph("Financial Data Table:")
# Table would be added here for wide data

doc.save("business_report.docx")

This example shows how to structure a professional document. Proper page setup makes reports more readable.

Common Page Setup Scenarios

Different documents need different layouts. Here are some common configurations.

For formal letters use wider left margins. This allows space for binding or punching.

Technical manuals often use smaller margins. This maximizes space for content and diagrams.

Presentations might use landscape orientation throughout. This matches projector screen proportions.

Best Practices for Page Setup

Always consider your document's purpose. Choose margins that suit your content type.

Test your layout with sample content. Make sure text remains readable and professional.

Use consistent margins throughout similar documents. This maintains brand consistency.

Consider combining page setup with other formatting techniques. Check our Python-docx Headers Footers Guide for complete document control.

Troubleshooting Page Setup Issues

Sometimes changes don't appear as expected. Here are common solutions.

Ensure you're modifying the correct section. Remember document.sections[0] is the first section.

Check that values are in the correct units. Always use Inches() for margin and size values.

Verify that orientation changes are applied before content addition. Setup should happen early.

For advanced formatting needs, see our Python-docx Table Formatting Complete Guide.

Conclusion

Python-docx provides powerful page setup control. You can automate professional document creation.

Key takeaways include margin configuration, orientation changes, and section management. These skills help create polished, readable documents.

Combine page setup with other Python-docx features for complete automation. Learn more in our Batch Generate docx Files in Python guide.

Start experimenting with different layouts. You will quickly master document formatting.