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.
Generate professional DOCX files using python-docx.
| 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 |
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: Khaled Shihab | 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')
pip install python-docx