Last modified: Nov 08, 2025 By Alexander Williams
Python-docx Tutorial: Create Word Documents
Python makes document creation easy. The python-docx library helps you generate Word files. This tutorial shows you how to use it.
You will learn to create documents from scratch. We cover paragraphs, tables, and formatting. Follow along with code examples.
What is Python-docx?
Python-docx is a Python library. It creates and modifies Microsoft Word files. These files have the .docx extension.
The library works with Word 2007 and later. It does not require Microsoft Word to be installed. You work entirely in Python.
Before starting, you need to Install Python-docx for Word Documents. This setup is quick and simple.
Installation and Setup
First, install the library using pip. Open your command line or terminal. Run the installation command below.
pip install python-docx
Verify the installation worked. Import the module in Python. No errors should appear.
import docx
print("Python-docx is ready!")
Python-docx is ready!
For detailed installation help, see Install python-docx for Word Documents in Python. This ensures proper setup.
Create Your First Document
Start by creating a new document. Use the Document() function. This creates an empty Word document.
from docx import Document
# Create a new document
doc = Document()
Now add a paragraph to your document. Use the add_paragraph() method. This inserts text content.
# Add a paragraph with text
paragraph = doc.add_paragraph('Hello, this is my first Word document created with Python!')
Save your document with a filename. Use the save() method. The file will be created in your current directory.
# Save the document
doc.save('my_first_document.docx')
Your first Word document is ready. Open it to see your text. This basic example shows the core process.
Working with Paragraphs
Paragraphs are fundamental in Word documents. You can add multiple paragraphs. Each can have different formatting.
Add several paragraphs in sequence. Each call to add_paragraph() creates a new paragraph.
doc = Document()
# Add multiple paragraphs
doc.add_paragraph('This is the first paragraph.')
doc.add_paragraph('This is the second paragraph.')
doc.add_paragraph('This is the third paragraph.')
doc.save('multiple_paragraphs.docx')
You can format paragraphs individually. Change alignment, spacing, and indentation. This makes your document look professional.
Text Formatting Options
Formatting makes your document visually appealing. You can apply bold, italic, and underline. Also change font size and color.
To format text, work with runs. A run is a sequence of characters with the same format. Use the add_run() method.
doc = Document()
# Add a paragraph
p = doc.add_paragraph('This text has ')
# Add formatted runs
p.add_run('bold').bold = True
p.add_run(' and ')
p.add_run('italic').italic = True
p.add_run(' formatting.')
doc.save('formatted_text.docx')
Formatting applies only to specific runs. The rest of the paragraph keeps default styling. This gives you precise control.
Adding Headings
Headings organize your document structure. Python-docx supports different heading levels. Use the add_heading() method.
doc = Document()
# Add headings of different levels
doc.add_heading('Main Title (Level 0)', 0)
doc.add_heading('Chapter 1 (Level 1)', 1)
doc.add_heading('Section 1.1 (Level 2)', 2)
doc.add_heading('Subsection 1.1.1 (Level 3)', 3)
doc.save('headings.docx')
Heading levels range from 0 to 9. Level 0 is the title. Level 1 is the main heading. Lower numbers mean more important headings.
Creating Tables
Tables display data in an organized way. Add tables to your Word documents. Use the add_table() method.
doc = Document()
# Create a table with 3 rows and 2 columns
table = doc.add_table(rows=3, cols=2)
# Add data to the table
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Age'
table.cell(1, 0).text = 'Alice'
table.cell(1, 1).text = '30'
table.cell(2, 0).text = 'Bob'
table.cell(2, 1).text = '25'
doc.save('table_example.docx')
Tables start with the specified dimensions. You can add or remove rows later. Access cells by their row and column indexes.
Adding Page Breaks
Page breaks control where content appears. Force a new page at specific points. Use the add_page_break() method.
doc = Document()
# Add content on first page
doc.add_paragraph('This is on page 1.')
# Add a page break
doc.add_page_break()
# Add content on second page
doc.add_paragraph('This is on page 2.')
doc.save('page_breaks.docx')
Page breaks ensure proper document flow. They are essential for multi-page documents. Use them between chapters or sections.
Complete Example: Business Report
Let's create a complete business report. This combines all the elements we've learned. It shows practical application.
from docx import Document
from docx.shared import Inches
doc = Document()
# Title
doc.add_heading('Quarterly Business Report', 0)
# Introduction
doc.add_paragraph('This report summarizes our Q3 performance.')
# Sales Data Table
doc.add_heading('Sales Data', 1)
table = doc.add_table(rows=4, cols=3)
table.cell(0, 0).text = 'Product'
table.cell(0, 1).text = 'Units Sold'
table.cell(0, 2).text = 'Revenue'
table.cell(1, 0).text = 'Widget A'
table.cell(1, 1).text = '150'
table.cell(1, 2).text = '$15,000'
table.cell(2, 0).text = 'Widget B'
table.cell(2, 1).text = '200'
table.cell(2, 2).text = '$20,000'
table.cell(3, 0).text = 'Widget C'
table.cell(3, 1).text = '75'
table.cell(3, 2).text = '$11,250'
# Conclusion with formatting
p = doc.add_paragraph('Overall, Q3 showed ')
p.add_run('strong growth').bold = True
p.add_run(' in all product categories.')
doc.save('business_report.docx')
This example creates a professional business report. It includes a title, headings, table, and formatted text. You can adapt it for your needs.
Conclusion
Python-docx is a powerful tool for Word document creation. You can automate report generation. Save time on manual document work.
Start with simple paragraphs and text. Then add formatting and tables. Finally, create complete documents with proper structure.
Practice with different document types. Try resumes, letters, or reports. The more you use python-docx, the more you'll discover its capabilities.
Remember to explore the official documentation for advanced features. You can add images, change styles, and work with existing documents too.