Last modified: Jun 08, 2025 By Alexander Williams
Install Python-docx for Word Documents
Python-docx is a powerful library for working with Word documents. It lets you create, edit, and format .docx files. This guide shows how to install and use it.
What is Python-docx?
Python-docx is a Python library for creating and updating Microsoft Word files. It supports paragraphs, tables, styles, and more. It works with .docx files only.
This library is useful for automating Word document tasks. It saves time and reduces manual work. You can generate reports, invoices, or any Word file dynamically.
Prerequisites
Before installing python-docx, ensure you have Python 3.6 or later. Check your Python version with:
import sys
print(sys.version)
3.9.7 (default, Sep 16 2021, 16:59:28)
[GCC 10.3.0]
You also need pip, Python's package installer. Verify it with pip --version
.
Install Python-docx
Install python-docx using pip. Run this command in your terminal:
pip install python-docx
This downloads and installs the latest version. For specific versions, use pip install python-docx==0.8.11
.
If you face permission issues, add --user
. This installs it for your user only:
pip install --user python-docx
Verify Installation
Check if python-docx installed correctly. Run Python and import the module:
import docx
print(docx.__version__)
0.8.11
No errors mean it's installed. The version number may differ based on your install.
Basic Usage Examples
Let's create a simple Word document. This code makes a new file with a heading and paragraph:
from docx import Document
# Create a new document
doc = Document()
# Add a heading
doc.add_heading('My First Document', 0)
# Add a paragraph
doc.add_paragraph('This is a simple Word document created with python-docx.')
# Save the document
doc.save('my_document.docx')
Run this script. It creates my_document.docx
in your working directory. Open it to see the result.
Common Features
Python-docx offers many features. Here are some common ones:
Add Tables
Tables help organize data. This example adds a 2x2 table:
from docx import Document
doc = Document()
table = doc.add_table(rows=2, cols=2)
# Add data
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Age'
table.cell(1, 0).text = 'John'
table.cell(1, 1).text = '30'
doc.save('table_document.docx')
Format Text
Change font style, size, and color. This makes text bold and italic:
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
doc = Document()
p = doc.add_paragraph()
run = p.add_run('Formatted Text')
run.bold = True
run.italic = True
doc.save('formatted.docx')
Troubleshooting
If you get errors, check these solutions:
ImportError: Ensure python-docx is installed. Reinstall if needed.
PermissionError: Run your script as admin or choose a writable directory.
AttributeError: You might be using an outdated method. Check the official docs.
Conclusion
Python-docx simplifies Word document automation. You learned how to install and use it. Try creating your own documents now.
For other Python libraries, check our guides on Py4J for Java or PyCUDA for GPU computing.