SoFunction
Updated on 2025-04-06

10 practical cases of python_docx library in Python

Word documents are one of our most commonly used formats in daily office and document processing. Python is a powerful programming language, throughpython - docxThe library can realize various automated operations on Word documents and greatly improve work efficiency. This article will share multiple practical cases to take you to experience the perfect combination of Python and Word.

1. Create a simple Word document

from docx import Document

def create_simple_doc():
    doc = Document()
    doc.add_heading('This is a new Word document', level=1)
    doc.add_paragraph('This is a sample text.  ')
    ('simple_doc.docx')

create_simple_doc()
print("Simple Word document was created successfully!")

explain

This script usespython - docxThe library creates a new Word document. First, importDocumentClass, it is the core class for manipulating Word documents. Next, create aDocumentObject, useadd_headingThe method adds a first-level title.add_paragraphMethod added a piece of text and finally usedsaveMethod to save the document. This feature is very practical for scenarios where simple template documents are generated quickly.

2. Insert an image in the document

from docx import Document
from  import Inches

def insert_image():
    doc = Document()
    doc.add_heading('Insert Image Example', level=1)
    doc.add_paragraph('The following is the inserted image:')
    doc.add_picture('', width=Inches(4))
    ('image_doc.docx')

insert_image()
print("The picture was successfully inserted into the Word document!")

explain

This script shows how to insert images in Word documents. passadd_pictureMethod implementation, wherewidthParameter usageInchesClass to specify the width of the image to make the image size meet the typesetting requirements. In scenarios such as making product introduction documents and technical reports that require pictures and texts, this function can make the document more vivid and intuitive.

3. Batch replacement of text in a document

from docx import Document

def replace_text(file_path, old_text, new_text):
    doc = Document(file_path)
    for paragraph in :
        if old_text in :
             = (old_text, new_text)
    for table in :
        for row in :
            for cell in :
                if old_text in :
                     = (old_text, new_text)
    (file_path)

replace_text('', 'Old text', 'New text')
print("Text replacement succeeded!")

explain

This script can batch replace text in Word documents. It iterates over paragraphs in the document and cells in the table, and replaces it with the new text once it finds that the old text contains the specified one. This feature can save a lot of time when dealing with a large number of similar documents and needing to modify the fixed content in it.

4. Extract text content from the document

from docx import Document

def extract_text(file_path):
    doc = Document(file_path)
    text = ""
    for paragraph in :
        text +=  + "\n"
    for table in :
        for row in :
            for cell in :
                text +=  + "\n"
    return text

extracted_text = extract_text('')
print(extracted_text)

explain

This script is used to extract all text content in Word documents, including text in paragraphs and tables. When you need to analyze the document content, count word frequency and other operations, you must first obtain the text in the document, which provides basic support.

5. Set the page layout of the document

from docx import Document
from  import Cm

def set_page_layout():
    doc = Document()
    section = [0]
    section.page_width = Cm(21)
    section.page_height = Cm(29.7)
    section.left_margin = Cm(2.54)
    section.right_margin = Cm(2.54)
    section.top_margin = Cm(2.54)
    section.bottom_margin = Cm(2.54)
    ('layout_doc.docx')

set_page_layout()
print("The page layout is set successfully!")

explain

This script is used to set the page layout of Word documents, including page size and page margins. passsectionObject to adjust related properties, useCmClass to specify the length unit to centimeters. When typing formal documents, such as papers and business reports, appropriate page layout can improve the professionalism and aesthetics of the documents.

6. Add a table to the document

from docx import Document

def add_table():
    doc = Document()
    doc.add_heading('Sample Table', level=1)
    table = doc.add_table(rows=3, cols=3)
     = 'Table Grid'
    hdr_cells = [0].cells
    hdr_cells[0].text = 'Column 1'
    hdr_cells[1].text = 'Column 2'
    hdr_cells[2].text = 'Column 3'
    for i in range(1, 3):
        row_cells = [i].cells
        row_cells[0].text = f'The{i}OK,The1List'
        row_cells[1].text = f'The{i}OK,The2List'
        row_cells[2].text = f'The{i}OK,The3List'
    ('table_doc.docx')

add_table()
print("The form was successfully added to the Word document!")

explain

This script shows the process of creating tables and populating content in Word documents. useadd_tableMethod create a 3 rows and 3 columns table, and set the table style toTable Grid, and then fill in the table header and table contents respectively. In scenarios such as creating data comparison documents, project schedules, etc., adding tables can clearly display the data.

7. Set the font format of the text

from docx import Document
from  import Pt
from  import WD_COLOR_INDEX

def set_font_style():
    doc = Document()
    p = doc.add_paragraph('This is a piece of text that has a font format.  ')
    run = [0]
     = 'Song style'
     = Pt(12)
     = True
     = True
     = True
     = RGBColor(0, 0, 255)
    .highlight_color = WD_COLOR_INDEX.YELLOW
    ('font_style_doc.docx')

set_font_style()
print("Font formatting is set successfully!")

explain

This script is used to set the font format of a piece of text in a Word document. passrunObjects to manipulate font properties, including font name, font size, bold, tilt, underline, color and highlight color, etc. Setting a unique font format can attract readers’ attention when it comes to emphasizing certain key content in the document.

8. Add header footer to the document

from docx import Document

def add_header_footer():
    doc = Document()
    section = [0]
    header = 
    footer = 
    [0].text = 'This is the header'
    [0].text = 'This is the footer content, page number:'
    footer.add_page_number()
    ('header_footer_doc.docx')

add_header_footer()
print("Header and footer added successfully!")

explain

This script adds header and footer to Word documents. passsectionThe object gets the header and footer, then adds text in the paragraphs of the header and footer, and adds page numbers in the footer. When creating multi-page documents, the header and footer can provide the identification information and page number of the document for easy reading and management.

9. Merge multiple Word documents

from docx import Document

def merge_docs(doc_paths, output_path):
    result_doc = Document()
    for doc_path in doc_paths:
        sub_doc = Document(doc_path)
        for element in sub_doc.:
            result_doc.(element)
    result_doc.save(output_path)

doc_paths = ['', '']
merge_docs(doc_paths,'merged_doc.docx')
print("Multiple Word documents merged successfully!")

explain

This script can combine multiple Word documents into one. It iterates through the content elements of each input document, appends it to the result document, and finally saves the merged document. In scenarios such as organizing project documents and compiling reports, merging multiple related documents can improve the integrity and readability of the document.

10. Generate personalized documents from templates

from docx import Document

def generate_personalized_doc(template_path, data):
    doc = Document(template_path)
    for paragraph in :
        for key, value in ():
            if key in :
                 = (key, value)
    for table in :
        for row in :
            for cell in :
                for key, value in ():
                    if key in :
                         = (key, value)
    ('personalized_doc.docx')

data = {'Name': 'Zhang San', 'age': '25', 'Position': 'engineer'}
generate_personalized_doc('', data)
print("Personalized document generation was successful!")

explain

This script generates personalized documents based on template documents. By replacing the placeholder in the template (e.g.Nameage) is the actual data to quickly generate personalized documents. In scenarios such as making batches of contracts, invitations, resumes, etc., using templates to generate personalized documents can greatly improve work efficiency.

Through the above cases, we can seepython - docxThe powerful and flexible library features when working with Word documents. Whether it is simple document creation or complex document content processing and formatting, Python can easily deal with it.

This is the article about the practical cases of the python_docx library in 10 Python. For more related content on python_docx, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!