Python教学基础:用Python和openpyxl结合Word模板域写入数据-由Deepseek产生

使用Python和openpyxl结合Word模板域写入数据

在Python中,我们可以使用openpyxl处理Excel数据,然后结合Word模板中的域(书签或合并域)来生成定制化的Word文档。以下是几种实现方法:

方法一:使用书签(Bookmarks)

1. 准备Word模板

首先创建一个Word模板,在需要插入数据的位置添加书签。

2. Python实现代码

python 复制代码
import openpyxl
from docx import Document
from docx.shared import Inches

def fill_word_template_from_excel(excel_file, word_template, output_file):
    # 读取Excel数据
    wb = openpyxl.load_workbook(excel_file)
    ws = wb.active
    
    # 读取Word模板
    doc = Document(word_template)
    
    # 假设Excel第一行是标题,第二行是数据
    data = {}
    for i, cell in enumerate(ws[1], 1):
        data[cell.value] = ws[2][i-1].value
    
    # 替换Word中的书签
    for paragraph in doc.paragraphs:
        for key, value in data.items():
            if key in paragraph.text:
                paragraph.text = paragraph.text.replace(f'[{key}]', str(value))
    
    # 处理表格
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                for key, value in data.items():
                    if key in cell.text:
                        cell.text = cell.text.replace(f'[{key}]', str(value))
    
    # 保存文档
    doc.save(output_file)
    print(f"文档已生成: {output_file}")

# 使用示例
fill_word_template_from_excel('data.xlsx', 'template.docx', 'output.docx')

方法二:使用邮件合并域

1. 准备Word模板

在Word模板中使用<<字段名>>格式的合并域。

2. Python实现代码

python 复制代码
import openpyxl
from docx import Document
import re

def mail_merge_from_excel(excel_file, word_template, output_file):
    # 读取Excel数据
    wb = openpyxl.load_workbook(excel_file)
    ws = wb.active
    
    # 读取Word模板
    doc = Document(word_template)
    
    # 获取数据(假设第一行是字段名)
    headers = [cell.value for cell in ws[1]]
    data_row = ws[2]  # 假设使用第二行数据
    
    # 创建数据字典
    data = {headers[i]: data_row[i].value for i in range(len(headers))}
    
    # 替换合并域
    for paragraph in doc.paragraphs:
        for key, value in data.items():
            merge_field = f'<<{key}>>'
            if merge_field in paragraph.text:
                paragraph.text = paragraph.text.replace(merge_field, str(value))
    
    # 处理表格中的合并域
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                for key, value in data.items():
                    merge_field = f'<<{key}>>'
                    if merge_field in cell.text:
                        cell.text = cell.text.replace(merge_field, str(value))
    
    # 保存文档
    doc.save(output_file)
    print(f"邮件合并完成: {output_file}")

# 使用示例
mail_merge_from_excel('employee_data.xlsx', 'offer_letter_template.docx', 'offer_letter.docx')

方法三:批量处理多条数据

python 复制代码
import openpyxl
from docx import Document
import os

def batch_generate_documents(excel_file, word_template, output_folder):
    # 创建输出文件夹
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 读取Excel数据
    wb = openpyxl.load_workbook(excel_file)
    ws = wb.active
    
    # 获取标题行
    headers = [cell.value for cell in ws[1]]
    
    # 遍历每一行数据
    for row_idx, row in enumerate(ws.iter_rows(min_row=2), 1):
        # 读取Word模板
        doc = Document(word_template)
        
        # 创建数据字典
        data = {headers[i]: row[i].value for i in range(len(headers))}
        
        # 替换文档内容
        for paragraph in doc.paragraphs:
            for key, value in data.items():
                placeholder = f'{{{key}}}'
                if placeholder in paragraph.text:
                    paragraph.text = paragraph.text.replace(placeholder, str(value))
        
        # 处理表格
        for table in doc.tables:
            for table_row in table.rows:
                for cell in table_row.cells:
                    for key, value in data.items():
                        placeholder = f'{{{key}}}'
                        if placeholder in cell.text:
                            cell.text = cell.text.replace(placeholder, str(value))
        
        # 保存文档
        filename = f"{output_folder}/document_{row_idx}.docx"
        doc.save(filename)
    
    print(f"批量生成完成,共生成 {ws.max_row - 1} 个文档")

# 使用示例
batch_generate_documents('students.xlsx', 'certificate_template.docx', 'certificates')

方法四:使用python-docx-template(推荐)

首先安装:pip install docxtpl

python 复制代码
import openpyxl
from docxtpl import DocxTemplate
import json

def advanced_template_filling(excel_file, word_template, output_file):
    # 读取Excel数据
    wb = openpyxl.load_workbook(excel_file)
    ws = wb.active
    
    # 准备数据
    headers = [cell.value for cell in ws[1]]
    data = {}
    
    # 假设我们处理第一行数据
    for i, header in enumerate(headers):
        data[header] = ws[2][i].value
    
    # 加载Word模板
    doc = DocxTemplate(word_template)
    
    # 渲染模板
    doc.render(data)
    
    # 保存文档
    doc.save(output_file)
    print(f"高级模板填充完成: {output_file}")

# 使用示例
advanced_template_filling('contract_data.xlsx', 'contract_template.docx', 'contract_final.docx')

完整案例:生成员工录用通知书

Excel数据 (employee_data.xlsx)

姓名 职位 部门 入职日期 薪资
张三 软件工程师 技术部 2024-01-15 15000

Word模板 (offer_letter_template.docx)

复制代码
员工录用通知书

尊敬的<<姓名>>先生/女士:

我们很高兴通知您,您已被录用为<<部门>>的<<职位>>。

入职日期:<<入职日期>>
基本薪资:¥<<薪资>>元/月

请携带相关材料按时报到。

人力资源部
<<当前日期>>

Python代码

python 复制代码
import openpyxl
from docxtpl import DocxTemplate
from datetime import datetime

def generate_offer_letters():
    # 读取Excel数据
    wb = openpyxl.load_workbook('employee_data.xlsx')
    ws = wb.active
    
    # 处理每一行数据
    for row in ws.iter_rows(min_row=2, values_only=True):
        name, position, department, start_date, salary = row
        
        # 准备上下文数据
        context = {
            '姓名': name,
            '职位': position,
            '部门': department,
            '入职日期': start_date.strftime('%Y年%m月%d日') if isinstance(start_date, datetime) else start_date,
            '薪资': salary,
            '当前日期': datetime.now().strftime('%Y年%m月%d日')
        }
        
        # 加载并渲染模板
        doc = DocxTemplate('offer_letter_template.docx')
        doc.render(context)
        
        # 保存文档
        output_file = f"offer_letter_{name}.docx"
        doc.save(output_file)
        print(f"已生成: {output_file}")

# 执行
generate_offer_letters()

总结

以上方法提供了不同复杂度的Word模板填充方案:

  1. 简单替换:使用字符串替换处理简单的占位符
  2. 邮件合并:处理标准的Word合并域格式
  3. 批量处理:一次性处理多条数据记录
  4. 高级模板:使用docxtpl库处理复杂的模板逻辑

选择哪种方法取决于你的具体需求:

  • 简单需求:使用方法一或二
  • 批量处理:使用方法三
  • 复杂模板:推荐使用方法四(docxtpl)

记得安装所需库:

bash 复制代码
pip install openpyxl python-docx docxtpl
相关推荐
Wise玩转AI6 小时前
Day 27|智能体的 UI 与用户交互层
人工智能·python·ui·ai·chatgpt·ai智能体
s***46986 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
runepic7 小时前
Python + PostgreSQL 批量图片分发脚本:分类、去重、断点续拷贝
服务器·数据库·python·postgresql
codists7 小时前
2025年11月文章一览
python
生而为虫7 小时前
31.Python语言进阶
python·scrapy·django·flask·fastapi·pygame·tornado
言之。7 小时前
Claude Code 实用开发手册
python
计算机毕设小月哥7 小时前
【Hadoop+Spark+python毕设】中国租房信息可视化分析系统、计算机毕业设计、包括数据爬取、Spark、数据分析、数据可视化、Hadoop
后端·python·mysql
2***c4357 小时前
Redis——使用 python 操作 redis 之从 hmse 迁移到 hset
数据库·redis·python
二川bro9 小时前
模型部署实战:Python结合ONNX与TensorRT
开发语言·python
秋邱9 小时前
AI + 社区服务:智慧老年康养助手(轻量化落地方案)
人工智能·python·重构·ar·推荐算法·agi