Document DOCX Skill - Quick Reference
This skill enables creation, editing, and analysis of .docx files for reports, contracts, proposals, documentation, and template-driven outputs.
Modern best practices (2026):
- Prefer templates + styles over manual formatting.
- Treat
.docx as the editable source; treat PDF as a release artifact.
- If distributing externally, include basic accessibility hygiene (headings, table headers, alt text).
Quick Reference
| Task |
Tool/Library |
Language |
When to Use |
| Create DOCX |
python-docx |
Python |
Reports, contracts, proposals |
| Create DOCX |
docx |
Node.js |
Server-side document generation |
| Convert to HTML |
mammoth.js |
Node.js |
Web display, content extraction |
| Parse DOCX |
python-docx |
Python |
Extract text, tables, metadata |
| Template fill |
docxtpl |
Python |
Mail merge, template-based generation |
| Review workflow |
Word compare, comments/highlights |
Any |
Human review without OOXML surgery |
| Tracked changes |
OOXML inspection, docx4j/OpenXML SDK/Aspose |
Any |
True redlines or parsing tracked changes |
Tool Selection
- Prefer
docxtpl when non-developers must edit layout/design in Word.
- Prefer
python-docx for structural edits (paragraphs/tables/headers/footers) when formatting complexity is moderate.
- Prefer
docx (Node.js) for server-side generation in TypeScript-heavy stacks.
- Prefer
mammoth for text-first extraction or DOCX-to-HTML (best effort; may drop some layout fidelity).
Known Limits (Plan Around These)
.doc (legacy) is not supported by these libraries; convert to .docx first (e.g., LibreOffice).
python-docx cannot reliably create true tracked changes; use Word compare or specialized OOXML tooling.
- Tables of Contents and many fields are placeholders until opened/updated in Word.
Core Operations
Create Document (Python - python-docx)
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
# Title
title = doc.add_heading('Document Title', 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Paragraph with formatting
para = doc.add_paragraph()
run = para.add_run('Bold and ')
run.bold = True
run = para.add_run('italic text.')
run.italic = True
# Table
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'
for i, row in enumerate(table.rows):
for j, cell in enumerate(row.cells):
cell.text = f'Row {i+1}, Col {j+1}'
# Image
doc.add_picture('image.png', width=Inches(4))
# Save
doc.save('output.docx')
Create Document (Node.js - docx)
import { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell } from 'docx';
import * as fs from 'fs';
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
children: [
new TextRun({ text: 'Bold text', bold: true }),
new TextRun({ text: ' and normal text.' }),
],
}),
new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph('Cell 1')] }),
new TableCell({ children: [new Paragraph('Cell 2')] }),
],
}),
],
}),
],
}],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync('output.docx', buffer);
});
Template-Based Generation (Python - docxtpl)
from docxtpl import DocxTemplate
doc = DocxTemplate('template.docx')
context = {
'company_name': 'Acme Corp',
'date': '2025-01-15',
'items': [
{'name': 'Widget A', 'price': 100},
{'name': 'Widget B', 'price': 200},
]
}
doc.render(context)
doc.save('filled_template.docx')
Extract Content (Python - python-docx)
from docx import Document
doc = Document('input.docx')
# Extract all text
full_text = []
for para in doc.paragraphs:
full_text.append(para.text)
# Extract tables
for table in doc.tables:
for row in table.rows:
row_data = [cell.text for cell in row.cells]
print(row_data)
Styling Reference
| Element |
Python Method |
Node.js Class |
| Heading 1 |
add_heading(text, 1) |
HeadingLevel.HEADING_1 |
| Bold |
run.bold = True |
TextRun({ bold: true }) |
| Italic |
run.italic = True |
TextRun({ italics: true }) |
| Font size |
run.font.size = Pt(12) |
TextRun({ size: 24 }) (half-points) |
| Alignment |
WD_ALIGN_PARAGRAPH.CENTER |
AlignmentType.CENTER |
| Page break |
doc.add_page_break() |
new PageBreak() |
Do / Avoid (Dec 2025)
Do
- Use consistent heading levels and a table of contents for long docs.
- Capture decisions and action items with owners and due dates.
- Store docs in a versioned, searchable system.
Avoid
- Manual formatting instead of styles (breaks consistency).
- Docs with no owner or review cadence (stale quickly).
- Copy/pasting without updating definitions and links.
Output Quality Checklist
- Structure: consistent heading hierarchy, styles, and (when needed) an auto-generated table of contents.
- Decisions: decisions/actions captured with owner + due date (not buried in prose).
- Versioning: doc ID + version + change summary; review cadence defined.
- Accessibility hygiene: headings/reading order are correct; table headers are marked; alt text for non-decorative images.
- Reuse: use
assets/doc-template-pack.md for decision logs and recurring doc types.
Optional: AI / Automation
Use only when explicitly requested and policy-compliant.
- Summarize meeting notes into decisions/actions; humans verify accuracy.
- Draft first-pass docs from outlines; do not invent facts or quotes.
Navigation
Resources
- references/docx-patterns.md - Advanced formatting, styles, headers/footers
- references/template-workflows.md - Mail merge, batch generation
- references/tracked-changes.md - Tracked changes: what is feasible, and what is not
- references/accessibility-compliance.md - WCAG 2.2 AA, reading order, alt text, EU EAA
- references/cross-platform-compatibility.md - Rendering across Word, Google Docs, LibreOffice
- references/document-automation-pipelines.md - CI/CD batch generation, quality gates
- data/sources.json - Library documentation links
Scripts
scripts/docx_inspect_ooxml.py - Dependency-free OOXML inspection (including tracked changes signals)
scripts/docx_extract.py - Extract text/tables to JSON (requires python-docx)
scripts/docx_render_template.py - Render a docxtpl template (requires docxtpl)
scripts/docx_to_html.mjs - Convert .docx to HTML (requires mammoth)
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
Related Skills
Fact-Checking
- Use web search/web fetch to verify current external facts, versions, pricing, deadlines, regulations, or platform behavior before final answers.
- Prefer primary sources; report source links and dates for volatile information.
- If web access is unavailable, state the limitation and mark guidance as unverified.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: document-docx3description: Create/edit .docx files with styles, tables, and templates. Use when asked to generate Word reports, contracts, proposals, or extract text. Use when this capability is needed.4---56# Document DOCX Skill - Quick Reference78This skill enables creation, editing, and analysis of `.docx` files for reports, contracts, proposals, documentation, and template-driven outputs.910Modern best practices (2026):11- Prefer templates + styles over manual formatting.12- Treat `.docx` as the editable source; treat PDF as a release artifact.13- If distributing externally, include basic accessibility hygiene (headings, table headers, alt text).1415## Quick Reference1617| Task | Tool/Library | Language | When to Use |18|------|--------------|----------|-------------|19| Create DOCX | python-docx | Python | Reports, contracts, proposals |20| Create DOCX | docx | Node.js | Server-side document generation |21| Convert to HTML | mammoth.js | Node.js | Web display, content extraction |22| Parse DOCX | python-docx | Python | Extract text, tables, metadata |23| Template fill | docxtpl | Python | Mail merge, template-based generation |24| Review workflow | Word compare, comments/highlights | Any | Human review without OOXML surgery |25| Tracked changes | OOXML inspection, docx4j/OpenXML SDK/Aspose | Any | True redlines or parsing tracked changes |2627## Tool Selection2829- Prefer `docxtpl` when non-developers must edit layout/design in Word.30- Prefer `python-docx` for structural edits (paragraphs/tables/headers/footers) when formatting complexity is moderate.31- Prefer `docx` (Node.js) for server-side generation in TypeScript-heavy stacks.32- Prefer `mammoth` for text-first extraction or DOCX-to-HTML (best effort; may drop some layout fidelity).3334## Known Limits (Plan Around These)3536- `.doc` (legacy) is not supported by these libraries; convert to `.docx` first (e.g., LibreOffice).37- `python-docx` cannot reliably create true tracked changes; use Word compare or specialized OOXML tooling.38- Tables of Contents and many fields are placeholders until opened/updated in Word.3940## Core Operations4142### Create Document (Python - python-docx)4344```python45from docx import Document46from docx.shared import Inches, Pt47from docx.enum.text import WD_ALIGN_PARAGRAPH4849doc = Document()5051# Title52title = doc.add_heading('Document Title', 0)53title.alignment = WD_ALIGN_PARAGRAPH.CENTER5455# Paragraph with formatting56para = doc.add_paragraph()57run = para.add_run('Bold and ')58run.bold = True59run = para.add_run('italic text.')60run.italic = True6162# Table63table = doc.add_table(rows=3, cols=3)64table.style = 'Table Grid'65for i, row in enumerate(table.rows):66 for j, cell in enumerate(row.cells):67 cell.text = f'Row {i+1}, Col {j+1}'6869# Image70doc.add_picture('image.png', width=Inches(4))7172# Save73doc.save('output.docx')74```7576### Create Document (Node.js - docx)7778```typescript79import { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell } from 'docx';80import * as fs from 'fs';8182const doc = new Document({83 sections: [{84 properties: {},85 children: [86 new Paragraph({87 children: [88 new TextRun({ text: 'Bold text', bold: true }),89 new TextRun({ text: ' and normal text.' }),90 ],91 }),92 new Table({93 rows: [94 new TableRow({95 children: [96 new TableCell({ children: [new Paragraph('Cell 1')] }),97 new TableCell({ children: [new Paragraph('Cell 2')] }),98 ],99 }),100 ],101 }),102 ],103 }],104});105106Packer.toBuffer(doc).then((buffer) => {107 fs.writeFileSync('output.docx', buffer);108});109```110111### Template-Based Generation (Python - docxtpl)112113```python114from docxtpl import DocxTemplate115116doc = DocxTemplate('template.docx')117context = {118 'company_name': 'Acme Corp',119 'date': '2025-01-15',120 'items': [121 {'name': 'Widget A', 'price': 100},122 {'name': 'Widget B', 'price': 200},123 ]124}125doc.render(context)126doc.save('filled_template.docx')127```128129### Extract Content (Python - python-docx)130131```python132from docx import Document133134doc = Document('input.docx')135136# Extract all text137full_text = []138for para in doc.paragraphs:139 full_text.append(para.text)140141# Extract tables142for table in doc.tables:143 for row in table.rows:144 row_data = [cell.text for cell in row.cells]145 print(row_data)146```147148## Styling Reference149150| Element | Python Method | Node.js Class |151|---------|---------------|---------------|152| Heading 1 | `add_heading(text, 1)` | `HeadingLevel.HEADING_1` |153| Bold | `run.bold = True` | `TextRun({ bold: true })` |154| Italic | `run.italic = True` | `TextRun({ italics: true })` |155| Font size | `run.font.size = Pt(12)` | `TextRun({ size: 24 })` (half-points) |156| Alignment | `WD_ALIGN_PARAGRAPH.CENTER` | `AlignmentType.CENTER` |157| Page break | `doc.add_page_break()` | `new PageBreak()` |158159## Do / Avoid (Dec 2025)160161### Do162163- Use consistent heading levels and a table of contents for long docs.164- Capture decisions and action items with owners and due dates.165- Store docs in a versioned, searchable system.166167### Avoid168169- Manual formatting instead of styles (breaks consistency).170- Docs with no owner or review cadence (stale quickly).171- Copy/pasting without updating definitions and links.172173## Output Quality Checklist174175- Structure: consistent heading hierarchy, styles, and (when needed) an auto-generated table of contents.176- Decisions: decisions/actions captured with owner + due date (not buried in prose).177- Versioning: doc ID + version + change summary; review cadence defined.178- Accessibility hygiene: headings/reading order are correct; table headers are marked; alt text for non-decorative images.179- Reuse: use `assets/doc-template-pack.md` for decision logs and recurring doc types.180181## Optional: AI / Automation182183Use only when explicitly requested and policy-compliant.184185- Summarize meeting notes into decisions/actions; humans verify accuracy.186- Draft first-pass docs from outlines; do not invent facts or quotes.187188## Navigation189190**Resources**191- [references/docx-patterns.md](references/docx-patterns.md) - Advanced formatting, styles, headers/footers192- [references/template-workflows.md](references/template-workflows.md) - Mail merge, batch generation193- [references/tracked-changes.md](references/tracked-changes.md) - Tracked changes: what is feasible, and what is not194- [references/accessibility-compliance.md](references/accessibility-compliance.md) - WCAG 2.2 AA, reading order, alt text, EU EAA195- [references/cross-platform-compatibility.md](references/cross-platform-compatibility.md) - Rendering across Word, Google Docs, LibreOffice196- [references/document-automation-pipelines.md](references/document-automation-pipelines.md) - CI/CD batch generation, quality gates197- [data/sources.json](data/sources.json) - Library documentation links198199**Scripts**200- `scripts/docx_inspect_ooxml.py` - Dependency-free OOXML inspection (including tracked changes signals)201- `scripts/docx_extract.py` - Extract text/tables to JSON (requires `python-docx`)202- `scripts/docx_render_template.py` - Render a `docxtpl` template (requires `docxtpl`)203- `scripts/docx_to_html.mjs` - Convert `.docx` to HTML (requires `mammoth`)204205**Templates**206- [assets/report-template.md](assets/report-template.md) - Standard report structure207- [assets/contract-template.md](assets/contract-template.md) - Legal document structure208- [assets/doc-template-pack.md](assets/doc-template-pack.md) - Decision log, meeting notes, changelog templates209210**Related Skills**211- [../document-pdf/SKILL.md](../document-pdf/SKILL.md) - PDF generation and conversion212- [../docs-codebase/SKILL.md](../docs-codebase/SKILL.md) - Technical writing patterns213214## Fact-Checking215216- Use web search/web fetch to verify current external facts, versions, pricing, deadlines, regulations, or platform behavior before final answers.217- Prefer primary sources; report source links and dates for volatile information.218- If web access is unavailable, state the limitation and mark guidance as unverified.219220---221> Converted and distributed by [TomeVault](https://tomevault.io/claim/vasilyu1983) — claim your Tome and manage your conversions.222<!-- tomevault:4.0:skill_md:2026-04-11 -->