Word Document Generator
Generate professional DOCX files using python-docx.
Rules (Mandatory)
- Document structure — title page, table of contents placeholder, numbered headings, page breaks between major sections
- No raw text dumps — use proper heading levels (Heading 1-4), styled paragraphs, and lists
- Tables for structured data — never present tabular data as plain text
- Consistent styling — fonts, colors, spacing uniform. Use document styles, not inline formatting
- Headers/footers — include document title in header, page numbers in footer
- File output — save to concrete path, verify file exists
Document Sections Pattern
| Section |
Style |
Purpose |
| Title Page |
Custom |
Title, subtitle, author, date, version |
| ToC Placeholder |
Heading |
"Table of Contents" (Word auto-generates on open) |
| Executive Summary |
Heading 1 + Normal |
1-paragraph overview |
| Body Sections |
Heading 1-3 + Normal |
Main content with sub-sections |
| Tables/Figures |
Table style + Caption |
Data presentation |
| Appendix |
Heading 1 + Normal |
Supporting material |
python-docx Patterns
from docx import Document
from docx.shared import Inches, Pt, RGBColor, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
doc = Document()
# Set default font
style = doc.styles['Normal']
font = style.font
font.name = 'Calibri'
font.size = Pt(11)
# Title
doc.add_heading('Document Title', level=0)
doc.add_paragraph('Author: <your-name> | Date: 2026-03-23')
doc.add_page_break()
# Sections
doc.add_heading('1. Executive Summary', level=1)
doc.add_paragraph('Overview text here.')
doc.add_heading('2. Analysis', level=1)
doc.add_heading('2.1 Market Data', level=2)
# Table
table = doc.add_table(rows=3, cols=3, style='Light Grid Accent 1')
table.rows[0].cells[0].text = 'Metric'
table.rows[0].cells[1].text = 'Q3'
table.rows[0].cells[2].text = 'Q4'
# Bullet list
for item in ['Point one', 'Point two', 'Point three']:
doc.add_paragraph(item, style='List Bullet')
# Header/Footer
section = doc.sections[0]
header = section.header
header.paragraphs[0].text = 'Document Title'
footer = section.footer
footer.paragraphs[0].text = 'Page ' # Word auto-adds number
doc.save('output.docx')
Workflow
- Outline — define heading structure (H1/H2/H3)
- Styles — set fonts, colors, spacing
- Content — add paragraphs, lists, tables
- Formatting — headers, footers, page breaks
- Save — write to file, verify
Install
pip install python-docx
1---2name: docx-generator3description: Generate professional Word documents with structured content, tables, headers, styles, and formatting. Uses python-docx. Use when creating reports, proposals, contracts, specs, or any structured document as DOCX/Word format.4---56# Word Document Generator78Generate professional DOCX files using `python-docx`.910## Rules (Mandatory)11121. **Document structure** — title page, table of contents placeholder, numbered headings, page breaks between major sections132. **No raw text dumps** — use proper heading levels (Heading 1-4), styled paragraphs, and lists143. **Tables for structured data** — never present tabular data as plain text154. **Consistent styling** — fonts, colors, spacing uniform. Use document styles, not inline formatting165. **Headers/footers** — include document title in header, page numbers in footer176. **File output** — save to concrete path, verify file exists1819## Document Sections Pattern2021| Section | Style | Purpose |22|---------|-------|---------|23| Title Page | Custom | Title, subtitle, author, date, version |24| ToC Placeholder | Heading | "Table of Contents" (Word auto-generates on open) |25| Executive Summary | Heading 1 + Normal | 1-paragraph overview |26| Body Sections | Heading 1-3 + Normal | Main content with sub-sections |27| Tables/Figures | Table style + Caption | Data presentation |28| Appendix | Heading 1 + Normal | Supporting material |2930## python-docx Patterns3132```python33from docx import Document34from docx.shared import Inches, Pt, RGBColor, Cm35from docx.enum.text import WD_ALIGN_PARAGRAPH36from docx.enum.table import WD_TABLE_ALIGNMENT3738doc = Document()3940# Set default font41style = doc.styles['Normal']42font = style.font43font.name = 'Calibri'44font.size = Pt(11)4546# Title47doc.add_heading('Document Title', level=0)48doc.add_paragraph('Author: <your-name> | Date: 2026-03-23')49doc.add_page_break()5051# Sections52doc.add_heading('1. Executive Summary', level=1)53doc.add_paragraph('Overview text here.')5455doc.add_heading('2. Analysis', level=1)56doc.add_heading('2.1 Market Data', level=2)5758# Table59table = doc.add_table(rows=3, cols=3, style='Light Grid Accent 1')60table.rows[0].cells[0].text = 'Metric'61table.rows[0].cells[1].text = 'Q3'62table.rows[0].cells[2].text = 'Q4'6364# Bullet list65for item in ['Point one', 'Point two', 'Point three']:66 doc.add_paragraph(item, style='List Bullet')6768# Header/Footer69section = doc.sections[0]70header = section.header71header.paragraphs[0].text = 'Document Title'72footer = section.footer73footer.paragraphs[0].text = 'Page ' # Word auto-adds number7475doc.save('output.docx')76```7778## Workflow79801. **Outline** — define heading structure (H1/H2/H3)812. **Styles** — set fonts, colors, spacing823. **Content** — add paragraphs, lists, tables834. **Formatting** — headers, footers, page breaks845. **Save** — write to file, verify8586## Install8788```bash89pip install python-docx90```