Fundamentals 5 min read

Using python-docx to Create and Manipulate Word Documents in Python

This article introduces the python-docx library, explains how to install it, and provides a complete code example that demonstrates creating a Word document with headings, styled paragraphs, images, tables, and saving the file, helping Python developers automate document processing.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using python-docx to Create and Manipulate Word Documents in Python

Today we share the third‑party Python library python-docx for handling Word documents.

python-docx is a Python library for creating and updating Microsoft Word (.docx) files, useful for automating document generation and manipulation.

Installation can be done via pip: pip install python-docx or pip3 install python-docx if both Python 2 and Python 3 are present.

The official documentation provides a full API reference and example files that showcase all features of python-docx.

Below is a complete example that demonstrates importing the library, creating a document, adding headings, paragraphs, styled text, an image, a table, a page break, and saving the file.

# 1、导入python-docx库
from docx import Document
from docx.shared import Inches

document = Document()
document.add_heading('Document Title', 0)

# 2、新建wrod文档、一级、二级、三级标题、自然段
p = document.add_paragraph('A plain paragraph having some ')

# 3、设置字体格式
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph('first item in unordered list', style='List Bullet')
document.add_paragraph('first item in ordered list', style='List Number')

# 4、在指定位置添加图片
document.add_picture('monty-truth.png', width=Inches(1.25))

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)
# 5、在指定位置添加表格
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

document.add_page_break()
# 6、文档另存为
document.save('demo.docx')

Additional examples and resources can be found on the python-docx GitHub page.

Tutorialdocument processingwordpython-docx
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.