Create and edit Word documents with python-docx
(MIT licensed).
When to Use
- Generate a
.docxreport, letter, memo, or template. - Edit an existing Word document (add/replace content, tables, images).
- Extract text or tables from a
.docx.
To convert a
.docxto Markdown for analysis, usemarkitdown-converter. Use this skill to author or edit Word files.
Setup
pip install python-docx
Create a document
from docx import Document
from docx.shared import Pt, Inches
doc = Document()
doc.add_heading("Quarterly Report", level=0) # title
doc.add_heading("Summary", level=1)
p = doc.add_paragraph("Revenue grew ")
p.add_run("15%").bold = True
p.add_run(" quarter over quarter.")
# table
table = doc.add_table(rows=1, cols=3)
table.style = "Light Grid Accent 1"
hdr = table.rows[0].cells
hdr[0].text, hdr[1].text, hdr[2].text = "Product", "Q1", "Q2"
for name, q1, q2 in [("Email Course", "19", "34"), ("Workflow Kit", "40", "53")]:
cells = table.add_row().cells
cells[0].text, cells[1].text, cells[2].text = name, q1, q2
doc.add_picture("chart.png", width=Inches(5)) # optional
doc.add_page_break()
doc.save("report.docx")
Read / extract
from docx import Document
doc = Document("report.docx")
text = "\n".join(p.text for p in doc.paragraphs)
for table in doc.tables:
for row in table.rows:
print([c.text for c in row.cells])
Edit an existing document
Open it, mutate paragraphs/runs/tables, and save() (to a new path to keep the
original). For find-and-replace, iterate doc.paragraphs and each
paragraph.runs, replacing run.text.
Notes & Limits
python-docxdoes not render to PDF; use LibreOffice/Word for conversion.- Editing complex tracked-changes or footnote structures is limited.
Credits
Original skill by Rinu (l3ad3r1) in collaboration with Claude (Anthropic). Powered by python-docx (MIT) — all credit for the underlying capability belongs to its authors.