python-docx
python-docx is a Python library for creating, reading, and updating Microsoft Word (.docx) files. It allows programmatic manipulation of Word documents without requiring Microsoft Word or similar software to be installed. The current version is 1.2.0, and the project is actively maintained.
Warnings
- breaking API Incompatibility with older versions: `python-docx` versions 0.3.0 and later are not API-compatible with prior versions. Significant changes were introduced.
- gotcha Styles applied via `python-docx` might not appear in the generated document if the style definition is not present in the starting .docx file (template). Word silently ignores undefined styles.
- gotcha Overwriting files: When opening and saving a document with the same filename, `python-docx` will overwrite the original file without any warning or confirmation.
- gotcha PermissionError when saving: You may encounter 'PermissionError: [Errno 13] Permission denied' if the .docx file you are trying to save is open in Microsoft Word or another application, or if your script lacks sufficient write permissions for the target directory.
- gotcha Complex tables and templates: Manipulating documents, especially those created from templates with complex tables (e.g., merged cells), can lead to unexpected XML errors or issues with content insertion and preservation.
Install
-
pip install python-docx
Imports
- Document
from docx import Document
Quickstart
from docx import Document
# Create a new document
document = Document()
# Add a heading
document.add_heading('My Awesome Document', level=0)
# Add a paragraph
p = document.add_paragraph('This is a simple paragraph with some ')
p.add_run('bold text').bold = True
p.add_run(' and some ')
p.add_run('italic text').italic = True
# Add a table
table = document.add_table(rows=1, cols=3)
table.style = 'Light Shading Accent 1'
heading_cells = table.rows[0].cells
heading_cells[0].text = 'Qty'
heading_cells[1].text = 'Item'
heading_cells[2].text = 'Price'
# Add more rows to the table
row_cells = table.add_row().cells
row_cells[0].text = '1'
row_cells[1].text = 'Widget A'
row_cells[2].text = '$10.00'
# Save the document
document.save('my_example.docx')
print("Document 'my_example.docx' created successfully.")