Document DOCX Skill - Quick Reference
This skill covers creation, editing, review, extraction, and release workflows for .docx documents.
Modern best practices (Jul 2026):
- Treat
.docx as the editable source and PDF as a release artifact.
- Prefer templates and built-in styles over manual formatting.
- Use comments for review notes; use Word Compare for true redlines.
- For LLM/RAG extraction, optimize for structure, trust level, and sanitization rather than visual fidelity.
- Treat macro-enabled Office files (
.docm, .dotm) as untrusted by default.
- Before promising a feature (comments, alt text, tracked changes), check the installed library version — several of these APIs are recent additions and silently absent on older pins. See "Version-Gate Before Promising A Feature" below.
Core Decision Rules (2026)
- If non-developers need to own layout/design, prefer
docxtpl with a Word-authored template.
- If the stack is Python and edits are structural, prefer
python-docx.
- If the stack is TypeScript/Node and the output is generated server-side, prefer
docx.
- If you need semantic HTML from a trusted document, prefer
mammoth, then sanitize before rendering or storing the output.
- If you need Markdown/JSON for search, indexing, or RAG, prefer
MarkItDown or Docling.
- If the user asks for tracked changes, do not promise high-level library support. Generate a revised
.docx and use Word Compare, or switch to OOXML-specialized tooling.
- If the user asks for PDF output, prefer Word automation for highest fidelity and LibreOffice headless for cross-platform batch workflows.
- If the input is
.doc, convert to .docx first. If it is .docm or .dotm, do not trust embedded macros.
Quick Reference
| Task |
Tool/Library |
Language |
When to Use |
| Create/edit DOCX |
python-docx |
Python |
Structural edits, reports, contracts, section/table/image work |
| Create/edit DOCX |
docx |
Node.js |
Server-side generation in TypeScript-heavy stacks |
| Template fill |
docxtpl |
Python |
Word-authored templates, mail merge, batch documents |
| Add/access comments |
python-docx + Word review workflow |
Python / Word |
Review notes without tracked revisions |
| Convert DOCX to HTML |
mammoth |
Node.js |
Semantic HTML from trusted documents |
| Convert DOCX to Markdown |
MarkItDown |
Python |
LLM/RAG ingestion where Markdown is preferred |
| Convert DOCX to Markdown/HTML/JSON |
Docling |
Python / CLI |
Multi-format ingestion, structured extraction, batch conversion |
| Parse text/tables/metadata |
python-docx + OOXML inspection |
Python |
Extraction, audits, migration tooling |
| Parse tracked changes/comments |
OOXML, Open XML SDK, docx4j, Aspose.Words |
Python / .NET / Java |
Revision-heavy workflows and interoperability edge cases |
| Convert DOCX to PDF |
Word automation / LibreOffice headless |
OS tooling |
Release artifacts and cross-platform smoke checks |
Selection Guide
- Prefer
docxtpl when a legal, ops, or business user needs to maintain the template in Word.
- Prefer
python-docx for moderate formatting complexity where you control the document structure in code.
- Prefer
docx when the surrounding service and tests already live in Node.js.
- Prefer
mammoth for trusted, text-first conversion to HTML; it is not a fidelity-preserving renderer.
- Prefer
MarkItDown for simple DOCX-to-Markdown pipelines.
- Prefer
Docling when DOCX is only one input among many formats or you need HTML/JSON/Markdown/text output from a unified pipeline.
ASCII Flow
DOCX request
|
v
Classify file + trust level
|-- .docx / .dotx -----> normal OOXML workflow
|-- .doc -------------> convert to .docx first
|-- .docm / .dotm ----> treat macros as untrusted
|
v
Choose lane
|-- Word-owned template ------> docxtpl
|-- Python structural edit ---> python-docx
|-- Node service generation --> docx
|-- trusted HTML conversion --> mammoth + sanitizer
|-- Markdown / JSON ingest ---> MarkItDown or Docling
|-- tracked-change review ----> revised DOCX + Word Compare
|
v
Generate, edit, or extract
|
v
Quality gate
|-- parseability + unresolved tags ---> scripts/docx_quality_gate.py
|-- comments / revisions / OOXML ----> scripts/docx_inspect_ooxml.py
|
v
Viewer, accessibility, and release checks
Format And Safety Caveats
.docx and .dotx are Office Open XML packages; .doc is legacy binary and needs conversion first.
.docm and .dotm are macro-enabled; do not treat them as safe content inputs.
python-docx can add and read comments in the main document body, but not threaded replies/resolved states, and not comment anchors in headers/footers.
python-docx does not provide reliable tracked-change authoring.
mammoth performs no sanitization of generated HTML or links from untrusted source documents.
- Tables of contents and many Word fields are placeholders until updated in Word.
Version-Gate Before Promising A Feature
Do not assume the environment has a current library. This is the single most common way this skill causes a confident-but-wrong answer:
Document.add_comment() only exists from python-docx 1.2.0 onward. On an older pinned version it raises AttributeError, not a graceful fallback. Check first: python -c "import docx; print(docx.__version__)".
- python-docx still has no public high-level property for image alt text (no
.alt_text on InlineShape) as of the current 1.x line — InlineShape only documents height, width, and type. The OOXML workaround in references/accessibility-compliance.md reaches into the private _inline attribute; treat that as an implementation detail that can move between releases, re-verify after any python-docx upgrade, and prefer python-docx's own comment/style APIs wherever a public one exists instead of private attributes.
- If a user asks for a feature this skill flags as unsupported (tracked-change authoring, threaded comment replies, resolved-state comments), say so plainly rather than approximating it with formatting hacks — a document that merely looks right (e.g., colored/struck-through text standing in for
<w:ins>/<w:del>) will fail any real redline/legal review because it carries no revision metadata.
Default Workflow
- Identify the file type and trust level:
.docx/.dotx vs .docm/.dotm vs legacy .doc.
- Pick the lane:
- Template generation ->
docxtpl
- Programmatic structure edits ->
python-docx or docx
- Review/comments -> comments or Word Compare
- LLM extraction ->
MarkItDown, Docling, or mammoth
- Generate or modify the document.
- Run
scripts/docx_quality_gate.py and, when needed, scripts/docx_inspect_ooxml.py.
- If shipping externally, validate rendering in Word plus at least one secondary viewer and apply accessibility hygiene.
Core Operations
Create A Document (Python - python-docx)
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches
doc = Document()
title = doc.add_heading("Quarterly Review", 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
doc.add_paragraph("Executive summary goes here.")
table = doc.add_table(rows=2, cols=2)
table.style = "Table Grid"
table.rows[0].cells[0].text = "Metric"
table.rows[0].cells[1].text = "Value"
table.rows[1].cells[0].text = "Revenue"
table.rows[1].cells[1].text = "$1.2M"
doc.add_picture("chart.png", width=Inches(4.5))
doc.save("quarterly-review.docx")
Add A Review Comment (Python - python-docx)
from docx import Document
doc = Document()
paragraph = doc.add_paragraph("This clause needs legal review.")
comment = doc.add_comment(
runs=paragraph.runs,
text="Clarify whether this applies to renewals as well.",
author="Legal",
initials="LG",
)
comment.paragraphs[0].add_run(" Add the renewal edge case explicitly.").bold = True
doc.save("reviewable.docx")
Fill A Template (Python - docxtpl)
from docxtpl import DocxTemplate
doc = DocxTemplate("template.docx")
context = {
"company_name": "Acme Corp",
"contract_date": "2026-03-13",
"items": [
{"name": "Widget A", "price": 100},
{"name": "Widget B", "price": 200},
],
}
doc.render(context)
doc.save("filled-template.docx")
Convert Trusted DOCX To HTML (Script)
node scripts/docx_to_html.mjs input.docx output.html --style-map custom-style-map.txt --extract-images-dir output-assets/
Extract Structure For Automation (Script)
python3 scripts/docx_extract.py input.docx --include headers footers hyperlinks comments images --out extracted.json
Output Quality Checklist
- Structure: heading hierarchy, list styles, and tables are intentional and consistent.
- Reviewability: comments or Word Compare are used for feedback-heavy workflows instead of ad hoc formatting hacks.
- Safety: macro-enabled files are treated as untrusted; HTML generated from DOCX is sanitized before use.
- Portability: fonts, numbering, tables, and images are checked in at least one non-Word viewer when documents are distributed.
- Accessibility hygiene: headings, descriptive links, table headers, document language, and alt text are present where needed.
- Release quality: run
scripts/docx_quality_gate.py before shipping or batch-publishing.
Optional: AI / Automation
Use only when explicitly requested and policy-compliant.
- Convert trusted DOCX content into Markdown/HTML/JSON for search or RAG.
- Summarize meeting notes into a Word template, but keep humans accountable for factual accuracy.
- Generate first-pass reports/contracts from structured data, then route through human review.
Navigation
Resources
- references/docx-patterns.md - Styles, headers/footers, tables, sections, TOC
- references/template-workflows.md - Template authoring, mail merge, batch rendering
- references/review-comments-workflows.md - Comments, review notes, Word Compare, comment limits
- references/tracked-changes.md - What is and is not feasible for tracked revisions
- references/llm-extraction-workflows.md - Mammoth, MarkItDown, Docling, HTML/Markdown/JSON extraction
- references/accessibility-compliance.md - Word accessibility, EN 301 549 context, manual checks
- references/cross-platform-compatibility.md - Word, Google Docs, LibreOffice, PDF conversion
- references/document-automation-pipelines.md - CI/CD, batch generation, quality gates
- data/sources.json - Current external documentation links
Scripts
scripts/docx_inspect_ooxml.py - Dependency-free OOXML inspection for tracked changes and comments
scripts/docx_extract.py - Extract text, tables, metadata, and optional headers/footers/hyperlinks/comments/images to JSON
scripts/docx_render_template.py - Render a docxtpl template from JSON
scripts/docx_to_html.mjs - Convert trusted .docx to HTML with style maps and optional image extraction
scripts/docx_quality_gate.py - Validate parseability, unresolved template tags, tracked-change/comment signals, and optional LibreOffice conversion
Templates
- assets/report-template.md - Standard report structure
- assets/contract-template.md - Legal document structure
- assets/doc-template-pack.md - Decision log, meeting notes, changelog templates
- assets/docx-template-authoring-checklist.md - Template authoring and handoff checklist
Related Skills
Fact-Checking
- Use
data/sources.json as the starting set of primary sources.
- Use web search/web fetch to verify current external facts, versions, release behavior, regulations, and platform quirks before final answers.
- Prefer primary documentation, package pages, release pages, and official standards pages.
- If web access is unavailable, state the limitation and mark volatile guidance as unverified.
Learnings Loop
Before applying this skill on a non-trivial task, read learnings.consolidated.md in this directory (and learnings.md if present).
After applying it, if you encountered a pattern worth remembering, a mistake worth preventing, or a domain fact that surprised you, append one dated bullet to learnings.md via agents-skills-feedback-loop/scripts/append_learning.py. Do not modify SKILL.md itself.
1---2name: document-docx3description: Create/edit .docx files with styles, templates, comments, and extraction workflows. Use when asked to generate Word reports, contracts, proposals, or convert Word content.4---5
6# Document DOCX Skill - Quick Reference
7
8This skill covers creation, editing, review, extraction, and release workflows for `.docx` documents.
9
10Modern best practices (Jul 2026):
11- Treat `.docx` as the editable source and PDF as a release artifact.
12- Prefer templates and built-in styles over manual formatting.
13- Use comments for review notes; use Word Compare for true redlines.
14- For LLM/RAG extraction, optimize for structure, trust level, and sanitization rather than visual fidelity.
15- Treat macro-enabled Office files (`.docm`, `.dotm`) as untrusted by default.
16- Before promising a feature (comments, alt text, tracked changes), check the installed library version — several of these APIs are recent additions and silently absent on older pins. See "Version-Gate Before Promising A Feature" below.
17
18## Core Decision Rules (2026)
19
20- If non-developers need to own layout/design, prefer `docxtpl` with a Word-authored template.
21- If the stack is Python and edits are structural, prefer `python-docx`.
22- If the stack is TypeScript/Node and the output is generated server-side, prefer `docx`.
23- If you need semantic HTML from a trusted document, prefer `mammoth`, then sanitize before rendering or storing the output.
24- If you need Markdown/JSON for search, indexing, or RAG, prefer `MarkItDown` or `Docling`.
25- If the user asks for tracked changes, do not promise high-level library support. Generate a revised `.docx` and use Word Compare, or switch to OOXML-specialized tooling.
26- If the user asks for PDF output, prefer Word automation for highest fidelity and LibreOffice headless for cross-platform batch workflows.
27- If the input is `.doc`, convert to `.docx` first. If it is `.docm` or `.dotm`, do not trust embedded macros.
28
29## Quick Reference
30
31| Task | Tool/Library | Language | When to Use |
32|------|--------------|----------|-------------|
33| Create/edit DOCX | `python-docx` | Python | Structural edits, reports, contracts, section/table/image work |
34| Create/edit DOCX | `docx` | Node.js | Server-side generation in TypeScript-heavy stacks |
35| Template fill | `docxtpl` | Python | Word-authored templates, mail merge, batch documents |
36| Add/access comments | `python-docx` + Word review workflow | Python / Word | Review notes without tracked revisions |
37| Convert DOCX to HTML | `mammoth` | Node.js | Semantic HTML from trusted documents |
38| Convert DOCX to Markdown | `MarkItDown` | Python | LLM/RAG ingestion where Markdown is preferred |
39| Convert DOCX to Markdown/HTML/JSON | `Docling` | Python / CLI | Multi-format ingestion, structured extraction, batch conversion |
40| Parse text/tables/metadata | `python-docx` + OOXML inspection | Python | Extraction, audits, migration tooling |
41| Parse tracked changes/comments | OOXML, Open XML SDK, docx4j, Aspose.Words | Python / .NET / Java | Revision-heavy workflows and interoperability edge cases |
42| Convert DOCX to PDF | Word automation / LibreOffice headless | OS tooling | Release artifacts and cross-platform smoke checks |
43
44## Selection Guide
45
46- Prefer `docxtpl` when a legal, ops, or business user needs to maintain the template in Word.
47- Prefer `python-docx` for moderate formatting complexity where you control the document structure in code.
48- Prefer `docx` when the surrounding service and tests already live in Node.js.
49- Prefer `mammoth` for trusted, text-first conversion to HTML; it is not a fidelity-preserving renderer.
50- Prefer `MarkItDown` for simple DOCX-to-Markdown pipelines.
51- Prefer `Docling` when DOCX is only one input among many formats or you need HTML/JSON/Markdown/text output from a unified pipeline.
52
53## ASCII Flow
54
55```text
56DOCX request
57 |
58 v
59Classify file + trust level
60 |-- .docx / .dotx -----> normal OOXML workflow
61 |-- .doc -------------> convert to .docx first
62 |-- .docm / .dotm ----> treat macros as untrusted
63 |
64 v
65Choose lane
66 |-- Word-owned template ------> docxtpl
67 |-- Python structural edit ---> python-docx
68 |-- Node service generation --> docx
69 |-- trusted HTML conversion --> mammoth + sanitizer
70 |-- Markdown / JSON ingest ---> MarkItDown or Docling
71 |-- tracked-change review ----> revised DOCX + Word Compare
72 |
73 v
74Generate, edit, or extract
75 |
76 v
77Quality gate
78 |-- parseability + unresolved tags ---> scripts/docx_quality_gate.py
79 |-- comments / revisions / OOXML ----> scripts/docx_inspect_ooxml.py
80 |
81 v
82Viewer, accessibility, and release checks
83```
84
85## Format And Safety Caveats
86
87- `.docx` and `.dotx` are Office Open XML packages; `.doc` is legacy binary and needs conversion first.
88- `.docm` and `.dotm` are macro-enabled; do not treat them as safe content inputs.
89- `python-docx` can add and read comments in the main document body, but not threaded replies/resolved states, and not comment anchors in headers/footers.
90- `python-docx` does not provide reliable tracked-change authoring.
91- `mammoth` performs no sanitization of generated HTML or links from untrusted source documents.
92- Tables of contents and many Word fields are placeholders until updated in Word.
93
94## Version-Gate Before Promising A Feature
95
96Do not assume the environment has a current library. This is the single most common way this skill causes a confident-but-wrong answer:
97
98- `Document.add_comment()` only exists from `python-docx` 1.2.0 onward. On an older pinned version it raises `AttributeError`, not a graceful fallback. Check first: `python -c "import docx; print(docx.__version__)"`.
99- python-docx still has no public high-level property for image alt text (no `.alt_text` on `InlineShape`) as of the current 1.x line — `InlineShape` only documents `height`, `width`, and `type`. The OOXML workaround in `references/accessibility-compliance.md` reaches into the private `_inline` attribute; treat that as an implementation detail that can move between releases, re-verify after any python-docx upgrade, and prefer `python-docx`'s own comment/style APIs wherever a public one exists instead of private attributes.
100- If a user asks for a feature this skill flags as unsupported (tracked-change authoring, threaded comment replies, resolved-state comments), say so plainly rather than approximating it with formatting hacks — a document that merely *looks* right (e.g., colored/struck-through text standing in for `<w:ins>`/`<w:del>`) will fail any real redline/legal review because it carries no revision metadata.
101
102## Default Workflow
103
1041. Identify the file type and trust level: `.docx`/`.dotx` vs `.docm`/`.dotm` vs legacy `.doc`.
1052. Pick the lane:
106 - Template generation -> `docxtpl`
107 - Programmatic structure edits -> `python-docx` or `docx`
108 - Review/comments -> comments or Word Compare
109 - LLM extraction -> `MarkItDown`, `Docling`, or `mammoth`
1103. Generate or modify the document.
1114. Run `scripts/docx_quality_gate.py` and, when needed, `scripts/docx_inspect_ooxml.py`.
1125. If shipping externally, validate rendering in Word plus at least one secondary viewer and apply accessibility hygiene.
113
114## Core Operations
115
116### Create A Document (Python - `python-docx`)
117
118```python
119from docx import Document
120from docx.enum.text import WD_ALIGN_PARAGRAPH
121from docx.shared import Inches
122
123doc = Document()
124
125title = doc.add_heading("Quarterly Review", 0)
126title.alignment = WD_ALIGN_PARAGRAPH.CENTER
127
128doc.add_paragraph("Executive summary goes here.")
129
130table = doc.add_table(rows=2, cols=2)
131table.style = "Table Grid"
132table.rows[0].cells[0].text = "Metric"
133table.rows[0].cells[1].text = "Value"
134table.rows[1].cells[0].text = "Revenue"
135table.rows[1].cells[1].text = "$1.2M"
136
137doc.add_picture("chart.png", width=Inches(4.5))
138doc.save("quarterly-review.docx")
139```
140
141### Add A Review Comment (Python - `python-docx`)
142
143```python
144from docx import Document
145
146doc = Document()
147paragraph = doc.add_paragraph("This clause needs legal review.")
148
149comment = doc.add_comment(
150 runs=paragraph.runs,
151 text="Clarify whether this applies to renewals as well.",
152 author="Legal",
153 initials="LG",
154)
155
156comment.paragraphs[0].add_run(" Add the renewal edge case explicitly.").bold = True
157doc.save("reviewable.docx")
158```
159
160### Fill A Template (Python - `docxtpl`)
161
162```python
163from docxtpl import DocxTemplate
164
165doc = DocxTemplate("template.docx")
166context = {
167 "company_name": "Acme Corp",
168 "contract_date": "2026-03-13",
169 "items": [
170 {"name": "Widget A", "price": 100},
171 {"name": "Widget B", "price": 200},
172 ],
173}
174doc.render(context)
175doc.save("filled-template.docx")
176```
177
178### Convert Trusted DOCX To HTML (Script)
179
180```bash
181node scripts/docx_to_html.mjs input.docx output.html --style-map custom-style-map.txt --extract-images-dir output-assets/
182```
183
184### Extract Structure For Automation (Script)
185
186```bash
187python3 scripts/docx_extract.py input.docx --include headers footers hyperlinks comments images --out extracted.json
188```
189
190## Output Quality Checklist
191
192- Structure: heading hierarchy, list styles, and tables are intentional and consistent.
193- Reviewability: comments or Word Compare are used for feedback-heavy workflows instead of ad hoc formatting hacks.
194- Safety: macro-enabled files are treated as untrusted; HTML generated from DOCX is sanitized before use.
195- Portability: fonts, numbering, tables, and images are checked in at least one non-Word viewer when documents are distributed.
196- Accessibility hygiene: headings, descriptive links, table headers, document language, and alt text are present where needed.
197- Release quality: run `scripts/docx_quality_gate.py` before shipping or batch-publishing.
198
199## Optional: AI / Automation
200
201Use only when explicitly requested and policy-compliant.
202
203- Convert trusted DOCX content into Markdown/HTML/JSON for search or RAG.
204- Summarize meeting notes into a Word template, but keep humans accountable for factual accuracy.
205- Generate first-pass reports/contracts from structured data, then route through human review.
206
207## Navigation
208
209**Resources**
210- [references/docx-patterns.md](references/docx-patterns.md) - Styles, headers/footers, tables, sections, TOC
211- [references/template-workflows.md](references/template-workflows.md) - Template authoring, mail merge, batch rendering
212- [references/review-comments-workflows.md](references/review-comments-workflows.md) - Comments, review notes, Word Compare, comment limits
213- [references/tracked-changes.md](references/tracked-changes.md) - What is and is not feasible for tracked revisions
214- [references/llm-extraction-workflows.md](references/llm-extraction-workflows.md) - Mammoth, MarkItDown, Docling, HTML/Markdown/JSON extraction
215- [references/accessibility-compliance.md](references/accessibility-compliance.md) - Word accessibility, EN 301 549 context, manual checks
216- [references/cross-platform-compatibility.md](references/cross-platform-compatibility.md) - Word, Google Docs, LibreOffice, PDF conversion
217- [references/document-automation-pipelines.md](references/document-automation-pipelines.md) - CI/CD, batch generation, quality gates
218- [data/sources.json](data/sources.json) - Current external documentation links
219
220**Scripts**
221- `scripts/docx_inspect_ooxml.py` - Dependency-free OOXML inspection for tracked changes and comments
222- `scripts/docx_extract.py` - Extract text, tables, metadata, and optional headers/footers/hyperlinks/comments/images to JSON
223- `scripts/docx_render_template.py` - Render a `docxtpl` template from JSON
224- `scripts/docx_to_html.mjs` - Convert trusted `.docx` to HTML with style maps and optional image extraction
225- `scripts/docx_quality_gate.py` - Validate parseability, unresolved template tags, tracked-change/comment signals, and optional LibreOffice conversion
226
227**Templates**
228- [assets/report-template.md](assets/report-template.md) - Standard report structure
229- [assets/contract-template.md](assets/contract-template.md) - Legal document structure
230- [assets/doc-template-pack.md](assets/doc-template-pack.md) - Decision log, meeting notes, changelog templates
231- [assets/docx-template-authoring-checklist.md](assets/docx-template-authoring-checklist.md) - Template authoring and handoff checklist
232
233**Related Skills**
234- [../document-pdf/SKILL.md](../document-pdf/SKILL.md) - PDF generation and release workflows
235- [../document-xlsx/SKILL.md](../document-xlsx/SKILL.md) - Spreadsheet generation and exports
236- [../document-pptx/SKILL.md](../document-pptx/SKILL.md) - Presentation generation
237- [../docs-codebase/SKILL.md](../docs-codebase/SKILL.md) - Technical writing patterns
238
239## Fact-Checking
240
241- Use `data/sources.json` as the starting set of primary sources.
242- Use web search/web fetch to verify current external facts, versions, release behavior, regulations, and platform quirks before final answers.
243- Prefer primary documentation, package pages, release pages, and official standards pages.
244- If web access is unavailable, state the limitation and mark volatile guidance as unverified.
245
246## Learnings Loop
247
248Before applying this skill on a non-trivial task, read `learnings.consolidated.md` in this directory (and `learnings.md` if present).
249
250After applying it, if you encountered a pattern worth remembering, a mistake worth preventing, or a domain fact that surprised you, append one dated bullet to `learnings.md` via `agents-skills-feedback-loop/scripts/append_learning.py`. Do not modify `SKILL.md` itself.
251