Skip to content

Creating Word Documents with python-docx

docx_create.py
from docx import Document
 
 
doc = Document()
doc.add_heading("Weekly Report", level=1)
doc.add_paragraph("This report was generated automatically.")
 
table = doc.add_table(rows=1, cols=2)
hdr = table.rows[0].cells
hdr[0].text = "Item"
hdr[1].text = "Value"
 
for item, value in [("Users", "120"), ("Errors", "3")]:
    row_cells = table.add_row().cells
    row_cells[0].text = item
    row_cells[1].text = value
 
doc.save("report.docx")
  • start from a template .docx for complex formatting
  • docx is not the same as PDF (convert separately if needed)

Generating a Word document means turning raw data into structured content and saving it to disk.

diagram Document generation flow mermaid
Steps python-docx follows to turn data into a Word file

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading