DOCX Creator Skill
Overview
Create and edit Microsoft Word (.docx) documents programmatically using python-docx. Handles documents from simple letters to complex reports with tables, headers, footers, and styles.
Library Setup
pip install python-docx
For reading complex .docx files: python-docx handles most cases. For advanced conversion (docx → PDF, docx → HTML), add libreoffice (CLI) or pandoc.
Step-by-Step Process
Step 1: Understand the Document Structure
Ask or infer:
- Document type (letter, report, contract, resume, technical doc)
- Sections and headings needed
- Whether it needs tables, images, or lists
- Target audience (formal vs. informal tone)
- Page setup (orientation, margins, headers/footers)
Step 2: Create a New Document
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
Step 3: Set Document Styles and Page Setup
from docx.oxml.ns import qn
import docx
# Set margins
sections = doc.sections
for section in sections:
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1.25)
section.right_margin = Inches(1.25)
Step 4: Add Headings
doc.add_heading("Document Title", level=0) # Title style
doc.add_heading("Section 1", level=1) # Heading 1
doc.add_heading("Subsection 1.1", level=2) # Heading 2
Step 5: Add Paragraphs with Formatting
para = doc.add_paragraph("This is a paragraph with ")
run = para.add_run("bold text")
run.bold = True
para.add_run(" and normal text.")
# Alignment
para.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
# Custom font
run.font.name = "Calibri"
run.font.size = Pt(11)
run.font.color.rgb = RGBColor(0x00, 0x00, 0x00)
Step 6: Add Tables
table = doc.add_table(rows=1, cols=3)
table.style = "Table Grid"
# Header row
header_cells = table.rows[0].cells
header_cells[0].text = "Name"
header_cells[1].text = "Role"
header_cells[2].text = "Department"
# Data rows
data = [("Alice", "Engineer", "Platform"), ("Bob", "Designer", "Product")]
for name, role, dept in data:
row_cells = table.add_row().cells
row_cells[0].text = name
row_cells[1].text = role
row_cells[2].text = dept
Step 7: Add Lists
# Bullet list
doc.add_paragraph("First item", style="List Bullet")
doc.add_paragraph("Second item", style="List Bullet")
# Numbered list
doc.add_paragraph("Step one", style="List Number")
doc.add_paragraph("Step two", style="List Number")
Step 8: Add Images
doc.add_picture("chart.png", width=Inches(5.0))
# Add caption
doc.add_paragraph("Figure 1: Quarterly Revenue", style="Caption")
Step 9: Add Headers and Footers
section = doc.sections[0]
header = section.header
header_para = header.paragraphs[0]
header_para.text = "Confidential - Company Name"
header_para.alignment = WD_ALIGN_PARAGRAPH.RIGHT
Step 10: Save
doc.save("output.docx")
Reading Existing .docx Files
doc = Document("existing.docx")
# Extract all text
for para in doc.paragraphs:
print(para.style.name, ":", para.text)
# Extract tables
for table in doc.tables:
for row in table.rows:
print([cell.text for cell in row.cells])
Edge Cases
- Template-based generation: Load an existing .docx as a template with
Document("template.docx")to inherit styles - Large documents: Add a page break with
doc.add_page_break()between major sections - Complex formatting: Use
doc.stylesto list available styles before applying custom ones - Track changes / comments:
python-docxdoes not support these - use LibreOffice CLI for conversion instead