Fundamentals 5 min read

Automate Word Document Generation from Excel Data with Python and docxtpl

This article walks through a Python automation case where data from an Excel sheet is inserted into a Word document using docxtpl, presenting the original attempt, an improved dynamic table solution, and complete code snippets to help readers implement similar workflows.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Automate Word Document Generation from Excel Data with Python and docxtpl

1. Introduction

In a Python community a user asked how to insert data from a specific Excel sheet into a Word document at a designated location without preserving table borders, i.e., plain text insertion.

The original attempt used docxtpl with a DataFrame read from the clipboard.

from docxtpl import DocxTemplate
import pandas as pd
df = pd.read_clipboard()
""
到期日 收款账号 打款额
350,000.00 NaN 15265838740016
100,000.00 2023/07/06 15265838740016
60,000.00 2023/08/13 15265838740016
270,000.00 2023/09/10 15265838740016
""
context = {"table": df}
docx = DocxTemplate('报告-模板.docx')
# 把键值对所对应的值赋予给键 render 赋予的意思
docx.render(context)
docx.save(r"报告-更新.docx")

2. Solution

Another member modified the Word template and reused the original code, then created an optimized version that uses a dynamic table template.

from docxtpl import DocxTemplate

tpl = DocxTemplate('templates/dynamic_table_tpl.docx')

context = {
    'col_labels': ['fruit', 'vegetable', 'stone', 'thing'],
    'tbl_contents': [
        {'label': 'yellow', 'cols': ['banana', 'capsicum', 'pyrite', 'taxi']},
        {'label': 'red', 'cols': ['apple', 'tomato', 'cinnabar', 'doubledecker']},
        {'label': 'green', 'cols': ['guava', 'cucumber', 'aventurine', 'card']},
    ],
}

tpl.render(context)
tpl.save('output/dynamic_table.docx')

3. Conclusion

The article demonstrates a practical Python automation workflow for generating Word documents from tabular data, providing reusable code snippets and tips that can be adapted to similar tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Word Automationdocxtplexcel-to-word
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

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.