
Creating Word documents programmatically is a common requirement in Python applications. Reports, invoices, contracts, audit logs, and exported datasets are often expected to be delivered as editable .docx files rather than plain text or PDFs.
Unlike plain text output, a Word document is a structured document composed of sections, paragraphs, styles, and layout rules. When generating Word documents in Python, treating .docx files as simple text containers quickly leads to layout issues and maintenance problems.
This tutorial focuses on practical Word document creation in Python using Spire.Doc for Python. It demonstrates how to construct documents using Word’s native object model, apply formatting at the correct structural level, and generate .docx files that remain stable and editable as content grows.
Content Overview
- 1. Understanding Word Document Structure in Python
- 2. Creating a Basic Word Document in Python
- 3. Adding and Formatting Text Content
- 4. Inserting Images into a Word Document
- 5. Creating and Populating Tables
- 6. Adding Headers and Footers
- 7. Controlling Page Layout with Sections
- 8. Setting Document Properties and Metadata
- 9. Saving, Exporting, and Performance Considerations
- 10. Common Pitfalls When Creating Word Documents in Python
1. Understanding Word Document Structure in Python
Before writing code, it is important to understand how a Word document is structured internally.
A .docx file is not a linear stream of text. It consists of multiple object layers, each with a specific responsibility:
- Document – the root container for the entire file
- Section – defines page-level layout such as size, margins, and orientation
- Paragraph – represents a logical block of text
- Run (TextRange) – an inline segment of text with character formatting
- Style – a reusable formatting definition applied to paragraphs or runs
When you create a Word document in Python, you are explicitly constructing this hierarchy in code. Formatting and layout behave predictably only when content is added at the appropriate level.
Spire.Doc for Python provides direct abstractions for these elements, allowing you to work with Word documents in a way that closely mirrors how Word itself organizes content.
2. Creating a Basic Word Document in Python
This section shows how to generate a valid Word document in Python using Spire.Doc. The example focuses on establishing the correct document structure and essential workflow.
Installing Spire.Doc for Python
pip install spire.doc
Alternatively, you can download Spire.Doc for Python and integrate it manually.
Creating a Simple .docx File
from spire.doc import Document, FileFormat
# Create the document container
document = Document()
# Add a section (defines page-level layout)
section = document.AddSection()
# Add a paragraph to the section
paragraph = section.AddParagraph()
paragraph.AppendText(
"This document was generated using Python. "
"It demonstrates basic Word document creation with Spire.Doc."
)
# Save the document
document.SaveToFile("basic_document.docx", FileFormat.Docx)
document.Close()
This example creates a minimal but valid .docx file that can be opened in Microsoft Word. It demonstrates the essential workflow: creating a document, adding a section, inserting a paragraph, and saving the file.

From a technical perspective:
- The Document object represents the Word file structure and manages its content.
- The Section defines the page-level layout context for paragraphs.
- The Paragraph contains the visible text and serves as the basic unit for all paragraph-level formatting.
All Word documents generated with Spire.Doc follow this same structural pattern, which forms the foundation for more advanced operations.
3. Adding and Formatting Text Content
Text in a Word document is organized hierarchically. Formatting can be applied at the paragraph level (controlling alignment, spacing, indentation, etc.) or the character level (controlling font, size, color, bold, italic, etc.). Styles provide a convenient way to store these formatting settings so they can be consistently applied to multiple paragraphs or text ranges without redefining the formatting each time. Understanding the distinction between paragraph formatting, character formatting, and styles is essential when creating or editing Word documents in Python.
Adding and Setting Paragraph Formatting
All visible text in a Word document must be added through paragraphs, which serve as containers for text and layout. Paragraph-level formatting controls alignment, spacing, and indentation, and can be set directly via the Paragraph.Format property. Character-level formatting, such as font size, bold, or color, can be applied to text ranges within the paragraph via the TextRange.CharacterFormat property.
from spire.doc import Document, HorizontalAlignment, FileFormat, Color
document = Document()
section = document.AddSection()
# Add the title paragraph
title = section.AddParagraph()
title.Format.HorizontalAlignment = HorizontalAlignment.Center
title.Format.AfterSpacing = 20 # Space after the title
title.Format.BeforeSpacing = 20
title_range = title.AppendText("Monthly Sales Report")
title_range.CharacterFormat.FontSize = 18
title_range.CharacterFormat.Bold = True
title_range.CharacterFormat.TextColor = Color.get_LightBlue()
# Add the body paragraph
body = section.AddParagraph()
body.Format.FirstLineIndent = 20
body_range = body.AppendText(
"This report provides an overview of monthly sales performance, "
"including revenue trends across different regions and product categories. "
"The data presented below is intended to support management decision-making."
)
body_range.CharacterFormat.FontSize = 12
# Save the document
document.SaveToFile("formatted_paragraph.docx", FileFormat.Docx)
document.Close()
Below is a preview of the generated Word document.

Technical notes
- Paragraph.Format sets alignment, spacing, and indentation for the entire paragraph
- AppendText() returns a TextRange object, which allows character-level formatting (font size, bold, color)
- Every paragraph must belong to a section, and paragraph order determines reading flow and pagination
Creating and Applying Styles
Styles allow you to define paragraph-level and character-level formatting once and reuse it across the document. They can store alignment, spacing, font, and text emphasis, making formatting more consistent and easier to maintain. Word documents support both custom styles and built-in styles, which must be added to the document before being applied.
Creating and Applying a Custom Paragraph Style
from spire.doc import (
Document, HorizontalAlignment, BuiltinStyle,
TextAlignment, ParagraphStyle, FileFormat
)
document = Document()
# Create a new custom paragraph style
custom_style = ParagraphStyle(document)
custom_style.Name = "CustomStyle"
custom_style.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center
custom_style.ParagraphFormat.TextAlignment = TextAlignment.Auto
custom_style.CharacterFormat.Bold = True
custom_style.CharacterFormat.FontSize = 20
# Inherit properties from a built-in heading style
custom_style.ApplyBaseStyle(BuiltinStyle.Heading1)
# Add the style to the document
document.Styles.Add(custom_style)
# Apply the custom style
title_para = document.AddSection().AddParagraph()
title_para.ApplyStyle(custom_style.Name)
title_para.AppendText("Regional Performance Overview")
Adding and Applying Built-in Styles
# Add a built-in style to the document
built_in_style = document.AddStyle(BuiltinStyle.Heading2)
document.Styles.Add(built_in_style)
# Apply the built-in style
heading_para = document.Sections.get_Item(0).AddParagraph()
heading_para.ApplyStyle(built_in_style.Name)
heading_para.AppendText("Sales by Region")
document.SaveToFile("document_styles.docx", FileFormat.Docx)
Preview of the generated Word document.

Technical Explanation
- ParagraphStyle(document) creates a reusable style object associated with the current document
- ParagraphFormat controls layout-related settings such as alignment and text flow
- CharacterFormat defines font-level properties like size and boldness
- ApplyBaseStyle() allows the custom style to inherit semantic meaning and default behavior from a built-in Word style
- Adding the style to document.Styles makes it available for use across all sections
Built-in styles, such as Heading 2, can be added explicitly and applied in the same way, ensuring the document remains compatible with Word features like outlines and tables of contents.
4. Inserting Images into a Word Document
In Word’s document model, images are embedded objects that belong to paragraphs, which ensures they flow naturally with text. Paragraph-anchored images adjust pagination automatically and maintain relative positioning when content changes.
Adding an Image to a Paragraph
from spire.doc import Document, TextWrappingStyle, HorizontalAlignment, FileFormat
document = Document()
section = document.AddSection()
section.AddParagraph().AppendText("\r\n\r\nExample Image\r\n")
# Insert an image
image_para = section.AddParagraph()
image_para.Format.HorizontalAlignment = HorizontalAlignment.Center
image = image_para.AppendPicture("Screen.jpg")
# Set the text wrapping style
image.TextWrappingStyle = TextWrappingStyle.Square
# Set the image size
image.Width = 350
image.Height = 200
# Set the transparency
image.FillTransparency(0.7)
# Set the horizontal alignment
image.HorizontalAlignment = HorizontalAlignment.Center
document.SaveToFile("document_images.docx", FileFormat.Docx)
Preview of the generated Word document.

Technical details
- AppendPicture() inserts the image into the paragraph, making it part of the text flow
- TextWrappingStyle determines how surrounding text wraps around the image
- Width and Height control the displayed size of the image
- FillTransparency() sets the image opacity
- HorizontalAlignment can center the image within the paragraph
Adding images to paragraphs ensures they behave like part of the text flow.
- Pagination adjusts automatically when images change size.
- Surrounding text reflows correctly when content is edited.
- When exporting to formats like PDF, images maintain their relative position.
These behaviors are consistent with Word’s handling of inline images.
For more advanced image operations in Word documents using Python, see how to insert images into a Word document with Python for a complete guide.
5. Creating and Populating Tables
Tables are commonly used to present structured data such as reports, summaries, and comparisons.
Internally, a table consists of rows, cells, and paragraphs inside each cell.
Creating and Formatting a Table in a Word Document
from spire.doc import Document, DefaultTableStyle, FileFormat, AutoFitBehaviorType
document = Document()
section = document.AddSection()
section.AddParagraph().AppendText("\r\n\r\nExample Table\r\n")
# Define the table data
table_headers = ["Region", "Product", "Units Sold", "Unit Price ($)", "Total Revenue ($)"]
table_data = [
["North", "Laptop", 120, 950, 114000],
["North", "Smartphone", 300, 500, 150000],
["South", "Laptop", 80, 950, 76000],
["South", "Smartphone", 200, 500, 100000],
["East", "Laptop", 150, 950, 142500],
["East", "Smartphone", 250, 500, 125000],
["West", "Laptop", 100, 950, 95000],
["West", "Smartphone", 220, 500, 110000]
]
# Add a table to the section
table = section.AddTable()
table.ResetCells(len(table_data) + 1, len(table_headers))
# Populate table headers
for col_index, header in enumerate(table_headers):
header_range = table.Rows[0].Cells[col_index].AddParagraph().AppendText(header)
header_range.CharacterFormat.FontSize = 14
header_range.CharacterFormat.Bold = True
# Populate table data
for row_index, row_data in enumerate(table_data):
for col_index, cell_data in enumerate(row_data):
data_range = table.Rows[row_index + 1].Cells[col_index].AddParagraph().AppendText(str(cell_data))
data_range.CharacterFormat.FontSize = 12
# Apply a default table style and auto-fit columns
table.ApplyStyle(DefaultTableStyle.ColorfulListAccent6)
table.AutoFit(AutoFitBehaviorType.AutoFitToContents)
document.SaveToFile("document_tables.docx", FileFormat.Docx)
Preview of the generated Word document.

Technical details
- Section.AddTable() inserts the table into the section content flow
- ResetCells(rows, columns) defines the table grid explicitly
- Table[row, column] or Table.Rows[row].Cells[col] returns a TableCell
Tables in Word are designed so that each cell acts as an independent content container. Text is always inserted through paragraphs, and each cell can contain multiple paragraphs, images, or formatted text. This structure allows tables to scale from simple grids to complex report layouts, making them flexible for reports, summaries, or any structured content.
For more detailed examples and advanced operations using Python, such as dynamically generating tables, merging cells, or formatting individual cells, see how to insert tables into Word documents with Python for a complete guide.
6. Adding Headers and Footers
Headers and footers in Word are section-level elements. They are not part of the main content flow and do not affect body pagination.
Each section owns its own header and footer, which allows different parts of a document to display different repeated content.
Adding Headers and Footers in a Section
from spire.doc import Document, FileFormat, HorizontalAlignment, FieldType, BreakType
document = Document()
section = document.AddSection()
section.AddParagraph().AppendBreak(BreakType.PageBreak)
# Add a header
header = section.HeadersFooters.Header
header_para1 = header.AddParagraph()
header_para1.AppendText("Monthly Sales Report").CharacterFormat.FontSize = 12
header_para1.Format.HorizontalAlignment = HorizontalAlignment.Left
header_para2 = header.AddParagraph()
header_para2.AppendText("Company Name").CharacterFormat.FontSize = 12
header_para2.Format.HorizontalAlignment = HorizontalAlignment.Right
# Add a footer with page numbers
footer = section.HeadersFooters.Footer
footer_para = footer.AddParagraph()
footer_para.Format.HorizontalAlignment = HorizontalAlignment.Center
footer_para.AppendText("Page ").CharacterFormat.FontSize = 12
footer_para.AppendField("PageNum", FieldType.FieldPage).CharacterFormat.FontSize = 12
footer_para.AppendText(" of ").CharacterFormat.FontSize = 12
footer_para.AppendField("NumPages", FieldType.FieldNumPages).CharacterFormat.FontSize = 12
document.SaveToFile("document_header_footer.docx", FileFormat.Docx)
document.Dispose()
Preview of the generated Word document.

Technical notes
- section.HeadersFooters.Header / .Footer provides access to header/footer of the section
- AppendField() inserts dynamic fields like FieldPage or FieldNumPages to display dynamic content
Headers and footers are commonly used for report titles, company information, and page numbering. They update automatically as the document changes and are compatible with Word, PDF, and other export formats.
For more detailed examples and advanced operations, see how to insert headers and footers in Word documents with Python.
7. Controlling Page Layout with Sections
In Spire.Doc for Python, all page-level layout settings are managed through the Section object. Page size, orientation, and margins are defined by the section’s PageSetup and apply to all content within that section.
Configuring Page Size and Orientation
from spire.doc import PageSize, PageOrientation
section.PageSetup.PageSize = PageSize.A4()
section.PageSetup.Orientation = PageOrientation.Portrait
Technical explanation
- PageSetup is a layout configuration object owned by the Section
- PageSize defines the physical dimensions of the page
- Orientation controls whether pages are rendered in portrait or landscape mode
PageSetup defines the layout for the entire section. All paragraphs, tables, and images added to the section will follow these settings. Changing PageSetup in one section does not affect other sections in the document, allowing different sections to have different page layouts.
Setting Page Margins
section.PageSetup.Margins.Top = 50
section.PageSetup.Margins.Bottom = 50
section.PageSetup.Margins.Left = 60
section.PageSetup.Margins.Right = 60
Technical explanation
- Margins defines the printable content area for the section
- Margin values are measured in document units
Margins control the body content area for the section. They are evaluated at the section level, so you do not need to set them for individual paragraphs, and header/footer areas are not affected.
Using Multiple Sections for Different Layouts
When a document requires different page layouts, additional sections must be created.
landscape_section = document.AddSection()
landscape_section.PageSetup.Orientation = PageOrientation.Landscape
Technical notes
- AddSection() creates a new section and appends it to the document
- Each section maintains its own PageSetup, headers, and footers
- Content added after this call belongs to the new section
Using multiple sections allows mixing portrait and landscape pages or applying different layouts within a single Word document.
Below is an example preview of the above settings in a Word document:

8. Setting Document Properties and Metadata
In addition to visible content, Word documents expose metadata through built-in document properties. These properties are stored at the document level and do not affect layout or rendering.
Assigning Built-in Document Properties
document.BuiltinDocumentProperties.Title = "Monthly Sales Report"
document.BuiltinDocumentProperties.Author = "Data Analytics System"
document.BuiltinDocumentProperties.Company = "Example Corp"
Technical notes
BuiltinDocumentPropertiesprovides access to standard document properties- Properties such as
Title,Author, andCompanycan be set programmatically
Document properties are commonly used for file indexing, search, document management, and audit workflows. In addition to built-in properties, Word documents support other metadata such as Keywords, Subject, Comments, and Hyperlink base. You can also define custom properties using Document.CustomDocumentProperties.
For a guide on managing document custom properties with Python, see how to manage custom metadata in Word documents with Python.
9. Saving, Exporting, and Performance Considerations
After constructing a Word document in memory, the final step is saving or exporting it to the required output format. Spire.Doc for Python supports multiple export formats through a unified API, allowing the same document structure to be reused without additional formatting logic.
Saving and Exporting Word Documents in Multiple Formats
A document can be saved as DOCX for editing or exported to other commonly used formats for distribution.
from spire.doc import FileFormat
document.SaveToFile("output.docx", FileFormat.Docx)
document.SaveToFile("output.pdf", FileFormat.PDF)
document.SaveToFile("output.html", FileFormat.Html)
document.SaveToFile("output.rtf", FileFormat.Rtf)
The export process preserves document structure, including sections, tables, images, headers, and footers, ensuring consistent layout across formats. Check out all the supported formats in the FileFormat enumeration.
Performance Considerations for Document Generation
For scenarios involving frequent or large-scale Word document generation, performance can be improved by:
- Reusing document templates and styles
- Avoiding unnecessary section creation
- Writing documents to disk only after all content has been generated
- After saving or exporting, explicitly releasing resources using document.Close()
When generating many similar documents with different data, mail merge is more efficient than inserting content programmatically for each file. Spire.Doc for Python provides built-in mail merge support for batch document generation. For details, see how to generate Word documents in bulk using mail merge in Python.
Saving and exporting are integral parts of Word document generation in Python. By using Spire.Doc for Python’s export capabilities and following basic performance practices, Word documents can be generated efficiently and reliably for both individual files and batch workflows.
10. Common Pitfalls When Creating Word Documents in Python
The following issues frequently occur when generating Word documents programmatically.
Treating Word Documents as Plain Text
Issue Formatting breaks when content length changes.
Recommendation Always work with sections, paragraphs, and styles rather than inserting raw text.
Hard-Coding Formatting Logic
Issue Global layout changes require editing multiple code locations.
Recommendation Centralize formatting rules using styles and section-level configuration.
Ignoring Section Boundaries
Issue Margins or orientation changes unexpectedly affect the entire document.
Recommendation Use separate sections to isolate layout rules.
11. Conclusion
Creating Word documents in Python involves more than writing text to a file. A .docx document is a structured object composed of sections, paragraphs, styles, and embedded elements.
By using Spire.Doc for Python and aligning code with Word’s document model, you can generate editable, well-structured Word files that remain stable as content and layout requirements evolve. This approach is especially suitable for backend services, reporting pipelines, and document automation systems.
For scenarios involving large documents or document conversion requirements, a licensed version is required.
