将excel文件中的信息读取后批量生成word文件

在日常办公过程中,可能需要把excel文件中的信息批量生成成百上千份word文档,便于打印、发邮件或存档等,比如根据excel中的合格人员招聘信息生成word合同文件,或是根据excel中的参会人员名单生成word参会通知等。

首先需要制作word模板,并在需要信息替换的位置设置域,选择MergeField,并配置域名。如本例设置了deptname和position两个域,需要从excel表格中读取后替换部门和职务的内容。还有年、月、日三个固定值的在程序里面直接写好后,填充在word中的year,month,day域。代码如下:

python 复制代码
import pandas as pd
from mailmerge import MailMerge
folder_path='.\\word\\' #文件夹相对路径
df=pd.read_excel(folder_path+'名单.xlsx') #通过read_excel方法读取excel数据存储到dataframe中
arr=df.to_numpy()   #将dataframe转换成二维数组
template=folder_path+'通知.docx' #word模版文件的位置
doc=MailMerge(template)
def merge(name,pos):
    doc.merge(deptname=name,
          position=pos,
          year='2024',
          month='11',
          day='27')
    doc.write(folder_path+f'{name}通知.docx')
for i in range(len(arr)):   #逐行遍历数组
    merge(arr[i][0],arr[i][1])
相关推荐
开开心心就好19 小时前
视频压缩工具推荐,绿色版免安装双击即用
java·前端·人工智能·spring·智能手机·intellij-idea·excel
泡海椒19 小时前
JQuick-Excel 导入反向映射实战:让中文表头稳定落到业务字段
开发语言·python·excel
Data-Miner20 小时前
本地化Excel智能工具怎么选?
excel
格数致用21 小时前
02- Excel文件导入导出技术实现详解:从原理到实战
excel·luckyexcel·报表处理
程序员清风2 天前
CSV、Excel 与数据库数据读取实践
数据库·oracle·excel
泡海椒2 天前
JQuick-Excel JContext 与 trans 实战:将字典值带入行转换
excel
2601_949950632 天前
练题簿在线免费刷题 从资料整理到考前自测
面试·职场和发展·pdf·word·刷题
泡海椒3 天前
JQuick-Excel dateFormat 转换实战:日期值与 FORMAT 显示格式的边界
开发语言·python·excel
鲲穹AI种草3 天前
Word 文档批量处理工具怎么选,多款工具实际使用情况整理
word·文档批量处理
2501_933670793 天前
2027风控策略岗秋招准备:SQL、Excel、建模的优先级与项目路径
人工智能·sql·excel