python-docx

Create, read, and update Microsoft Word .docx files programmatically in Python.

Library
PyPI
v1.2.0
5,700stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
55/100Fair
Development Activity36
Maintenance0
Community84
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
78/100Good
Architecture85
Code Quality80
Innovation65
Learning Curve80

python-docx is a Python library for creating, reading, and updating Microsoft Word 2007+ (.docx) documents. It exposes a Pythonic object model — Document, Paragraph, Run, Table, Section, Styles — that maps onto the underlying Office Open XML (OOXML) format, so generating a report or editing an existing document reads like ordinary Python rather than raw XML manipulation.

Under the hood it implements the Open Packaging Conventions (the zip-based container format Word files use) and a schema-driven layer of custom lxml element classes, giving it both a friendly high-level API for common tasks and a documented escape hatch into the raw XML tree for anything the high-level API doesn’t cover yet. It’s a long-running, widely used project (5,700+ GitHub stars, over a thousand commits) that has become the de facto standard for programmatic Word document generation in Python.

What You Get

  • A Document object model covering paragraphs, runs, tables, sections, styles, headers/footers, and inline images
  • Round-trip editing: open an existing .docx, modify specific paragraphs or table cells, and save without disturbing the rest of the document
  • Fine-grained text formatting through Run objects (bold, italic, font, color, size) and paragraph-level formatting (alignment, spacing, indentation)
  • A built-in default document template so Document() with no arguments produces a ready-to-use blank document
  • Direct access to the underlying OOXML tree via the oxml layer for formatting the high-level API doesn’t yet expose

Common Use Cases

  • Generating invoices, contracts, or reports as .docx files from application data
  • Mail-merge-style document generation — populating a Word template with per-recipient data in bulk
  • Extracting or auditing text and table content from a batch of existing Word documents
  • Programmatically applying consistent styles/branding across a set of generated documents

Under The Hood

Architecture The library is built in three layers: an opc/ layer implementing the Open Packaging Conventions (zip-based reading and writing of package parts via pkgreader.py, pkgwriter.py, part.py, and phys_pkg.py), an oxml/ layer of custom lxml element classes generated declaratively through xmlchemy.py’s BaseOxmlElement and descriptor system that mirrors the ECMA-376 OOXML schema, and a high-level docx/ API (document.py, text/, table.py, section.py, styles/) that wraps oxml objects in Pythonic classes. A call like document.add_paragraph() constructs an oxml element through this chain and returns a thin wrapper around it. Because every high-level class delegates storage and mutation to the oxml layer, changes to the core xmlchemy declarative element system ripple through nearly every public class.

Tech Stack python-docx is pure Python with two runtime dependencies — lxml (>=3.1.0) for XML parsing/serialization and typing_extensions for backported typing constructs — targeting Python 3.9+. It’s built with setuptools under a PEP 517 backend, manages development dependencies with uv (a committed uv.lock is present), and documents are built with Sphinx and published on Read the Docs. There’s no web or ORM framework involved; this is a standalone document-manipulation library.

Code Quality The project has 59 unit-test modules under tests/ that mirror the src/docx package layout, using pytest with a custom mock/fixture utility module and a compact CXML mini-language for building XML test fixtures, following a Describe*/it_* BDD naming convention. Alongside this, roughly 70 Gherkin .feature files under features/ are run through behave as end-to-end acceptance tests against real generated .docx files. Static typing is enforced with pyright and linting with ruff; errors are raised as explicit typed exceptions rather than swallowed. Only a legacy Travis CI config is present in this snapshot rather than GitHub Actions, though tox.ini supports local multi-version test runs.

What Makes It Unique The distinguishing technical choice is the xmlchemy declarative custom-element framework: instead of hand-writing lxml element subclasses, oxml elements are defined through descriptor-based helpers that map OOXML schema constructs (child elements, attributes, choice groups) directly onto Python class attributes. This keeps the low-level layer schema-faithful and DRY while keeping each high-level API class thin, and it gives users first-class, documented access to raw OOXML for anything the high-level API doesn’t cover. It’s a mature, disciplined pattern rather than a novel one — the value is in comprehensive, reliable coverage of the OOXML surface rather than a new paradigm.

Used by 13 apps in this directory

Python
100%
Apache 2.0

Agno

Devops · AI Development · Automation

41,969

Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.

View details
93
Repo Health
87
Technical
66
Dependency
Built with
Python100%
Updated today
Python
90%
Apache 2.0

Apache Airflow

Data Engineering

46,645

Define, schedule, and monitor complex data workflows as Python code — with a powerful UI, 80+ provider integrations, and battle-tested scalability across thousands of production deployments.

View details
96
Repo Health
89
Technical
65
Dependency
Built with
Python90%
Updated today
Python
67%
Other

AutoGPT

Automation · Productivity · AI Assistants

186,997

Build, deploy, and run autonomous AI agents that automate complex multi-step workflows using a visual block-based graph editor.

View details
93
Repo Health
78
Technical
67
Dependency
Built with
Python67%
TypeScript31%
Updated today
Python
67%
Apache 2.0

GPT Researcher

Productivity · AI Assistants

29,203

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
91
Repo Health
91
Technical
64
Dependency
Built with
Python67%
TypeScript20%
Updated 3 days ago
Python
100%
MIT

Graphify

AI Agents

112,429

A YC-backed, open-source knowledge graph skill for AI coding assistants — type /graphify and it maps your entire project (code, docs, PDFs, images, videos) into a queryable graph instead of grepping through files.

View details
83
Repo Health
73
Technical
73
Dependency
Built with
Python100%
Updated yesterday
Python
84%
Apache 2.0

knowhere

AI Development · Developer Tools

2,752

Transform messy, unstructured documents into persistent, navigable memory that AI agents can actually use.

View details
83
Repo Health
75
Technical
69
Dependency
Built with
Python84%
HTML15%
Updated today
Python
68%
MIT

Langflow

AI Agents · AI Development

153,880

Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.

View details
90
Repo Health
85
Technical
65
Dependency
Built with
Python68%
TypeScript23%
Updated today
Python
45%
GPL 3.0

MaxKB

AI Development · Knowledge Management

22,646

Build enterprise-grade AI agents with RAG, workflows & multi-modal support

View details
91
Repo Health
68
Technical
66
Dependency
Built with
Python45%
Vue37%
TypeScript17%
Updated yesterday
TypeScript
94%
Other

OpenHands

AI Code Assistants · AI Development

85,609

The self-hosted developer control center for running AI coding agents — locally, in Docker, on VMs, or across cloud backends — with automation workflows for GitHub, Slack, and more.

View details
91
Repo Health
82
Technical
71
Dependency
Built with
TypeScript94%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search