from docx import Document # 将真实的员工信息保存在字典中 person_list = [ { "name":"杨天偿", "id":"333222199909120987", "sdate":"2017年7月1日", "edate":"2021年11月1日", "department": }, { "name":"刘一奇", "id":"110120198909120937", "sdate":"2016年3月1日", "edate":"2020年10月1日", "departm拿会所" }, { "name":"张国涛", "id":"111222199908120987", "sdate "company":"深圳市金威源餐饮有限公司" }, {后勤部", "position":"大茶壶", "company":"深圳市怡红院高级会所" }, { "name":"张三", "id":"112221199909120987", "sd":"后勤部", "position":" }, { "name":"李四", "id":"112221199909120987", "sdate部", "position": } ] # 遍历列表。批量生成离职证明 for person in person_list: # 1.读取离职证明的模板文件 doc = Document(r"离职证明模板.docx") # 2.数据处理 for p in doc.paragraphs: # 过滤无需处理的段落 if "{" not in p.text: continue # 处理带有{的段落 for run in p.runs: # 过滤无需处理的run对象 if "{" not in run.text: continue print(run.text) key = run.text[1:-1] # 替换 run.text = run.text.replace(run.text,person[key]) # 3.将处理之后的数据进行保存 doc.save(f'{person["name"]}的离职证明.docx') # 说明:如果需要批量生成文件,则需提前准备模板【一定要考虑到段落的组成】,就可以借助于上述代码的思想操作
批量发文件
最新推荐文章于 2025-03-19 21:51:57 发布