python-docx: Create and Edit Word Documents with Python (2026)

Automation Oct 09, 2026

Watch on YouTube

Office automation is not only Excel and PDF. After covering openpyxl for Excel and pypdf for PDFs, the natural third pillar is Word. With python-docx 1.2.0 on Python 3.13.5 you can create .docx files, style runs, build tables, and open existing documents for edits — all without launching Microsoft Word.

Install python-docx

pip install python-docx

Confirm the install:

python -c "import docx; print(docx.__version__)"
# 1.2.0

Create a Document with headings, styled runs, and a table

Start from Document(), add structure, then save(). The script below builds a weekly sales report and prints the real file size and paragraph count from a live run.

from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from pathlib import Path

out = Path("weekly_report.docx")
doc = Document()

doc.add_heading("Weekly Sales Report", level=1)
doc.add_heading("Summary", level=2)

p = doc.add_paragraph()
run = p.add_run("Status: ")
run.bold = True
run = p.add_run("On track")
run.italic = True
run.font.color.rgb = RGBColor(0x00, 0x80, 0x00)

doc.add_paragraph(
    "This report was generated automatically with python-docx. "
    "Headings, styled runs, and a table are included below."
)

table = doc.add_table(rows=1, cols=3)
table.style = "Table Grid"
hdr = table.rows[0].cells
hdr[0].text = "Region"
hdr[1].text = "Units"
hdr[2].text = "Revenue"

for region, units, revenue in [
    ("North", "1,240", "$48,200"),
    ("South", "980", "$36,750"),
    ("East", "1,510", "$55,100"),
]:
    row = table.add_row().cells
    row[0].text = region
    row[1].text = units
    row[2].text = revenue

footer = doc.add_paragraph()
footer.alignment = WD_ALIGN_PARAGRAPH.CENTER
fr = footer.add_run("Generated with python-docx")
fr.font.size = Pt(9)
fr.font.color.rgb = RGBColor(0x66, 0x66, 0x66)

doc.save(out)
print(f"Saved: {out}")
print(f"File size: {out.stat().st_size} bytes")
print(f"Paragraphs: {len(doc.paragraphs)}")
print(f"Tables: {len(doc.tables)}")

Actual output (Python 3.13.5, python-docx 1.2.0):

Saved: weekly_report.docx
File size: 37024 bytes
Paragraphs: 6
Tables: 1

Key ideas:

  • add_heading(text, level=…) sets outline styles.
  • A paragraph is a container of runs; bold, italic, color, and font size apply per run.
  • add_table plus Table Grid gives a bordered table you can fill cell by cell.

Open and edit an existing .docx

Pass a path to Document(...) to load a file, append sections, then save under a new name (or overwrite).

from docx import Document
from pathlib import Path

path = Path("weekly_report.docx")
doc = Document(path)

doc.add_heading("Notes", level=2)
doc.add_paragraph(
    "East region led revenue this week. "
    "Review South inventory levels next sprint."
)

edited = path.with_name("weekly_report_edited.docx")
doc.save(edited)

check = Document(edited)
print(f"Opened: {path.name}")
print(f"Saved edited: {edited.name}")
print(f"Edited file size: {edited.stat().st_size} bytes")
print(f"Paragraphs after edit: {len(check.paragraphs)}")
print(f"Tables: {len(check.tables)}")
print("Paragraph preview:")
for i, p in enumerate(check.paragraphs[:8]):
    text = (p.text or "").strip()
    if text:
        print(f"  [{i}] {p.style.name}: {text[:70]}")

Actual output:

Opened: weekly_report.docx
Saved edited: weekly_report_edited.docx
Edited file size: 37082 bytes
Paragraphs after edit: 8
Tables: 1
Paragraph preview:
  [0] Heading 1: Weekly Sales Report
  [1] Heading 2: Summary
  [2] Normal: Status: On track
  [3] Normal: This report was generated automatically with python-docx. Headings, st
  [5] Normal: Generated with python-docx
  [6] Heading 2: Notes
  [7] Normal: East region led revenue this week. Review South inventory levels next

After the edit you have eight paragraphs and the original table intact — useful for report pipelines that inject a Notes block each week.

When to reach for python-docx

  • Batch-generating invoices, proposals, or status reports from Python data.
  • Filling templates: open a branded .docx, replace placeholders in paragraphs, save copies.
  • Combining with tabular workflows — for example exporting summaries alongside PyArrow tables and Parquet datasets.

python-docx does not render Word UI or run VBA macros; it reads and writes the Open XML package. That is exactly what you want for headless servers and CI jobs.

Quick checklist

  1. pip install python-docx
  2. Document() → headings → runs → table → save()
  3. Document(path) → mutate → save() again
  4. Assert file size / paragraph counts in tests so broken templates fail fast

With Excel (openpyxl), PDF (pypdf), and Word (python-docx) covered, your Python stack can drive the full office document loop from one venv.

Share:
Last updated: October 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices