将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])
相关推荐
qq_435139574 分钟前
EasyExcel(FastExcel)Excel导出功能 技术文档
excel
wtsolutions3 小时前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions3 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
fs哆哆5 小时前
VB.NET和VBA教程-如何查找Excel数据区域边界
excel
小矮强5 小时前
Excel中根据年月日提取月日,并按月日进行排序
excel
开开心心_Every5 小时前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别
wtsolutions6 小时前
Real-World Use Cases - How Organizations Use JSON to Excel
json·excel
wtsolutions6 小时前
Introduction to JSON to Excel - The Ultimate Conversion Tool
json·excel
骆驼爱记录7 小时前
Python打包命令全攻略
自动化·word·excel·新人首发
wtsolutions7 小时前
JSON to Excel WPS Add-in - Perfect for WPS Office Users
json·excel·wps