首页 > 其他 > 详细

批量生成不同合同(模块docx)

时间:2020-02-16 17:22:17      阅读:94      评论:0      收藏:0      [点我收藏+]

一、合同模板

技术分享图片

二、合同清单

技术分享图片

三、代码

 1 from docx import Document
 2 import xlrd
 3 
 4 def change_text(old_text, new_text):
 5     all_paragraphs = document.paragraphs # 选取段落
 6     for paragraph in all_paragraphs: # 对于段落的替换
 7         for run in paragraph.runs: # 选取段落中的块
 8             run_text = run.text.replace(old_text, new_text) # 替换
 9             run.text = run_text
10 
11     all_tables = document.tables
12     for table in all_tables: # 对于表格的替换
13         for row in table.rows:
14             for cell in row.cells:
15                 cell_text = cell.text.replace(old_text, new_text)
16                 cell.text = cell_text
17 
18 xlsx = xlrd.open_workbook(r"D:\python\合同清单.xlsx")
19 sheet = xlsx.sheet_by_index(0)
20 
21 for table_row in range(1, sheet.nrows):
22     document = Document(r"D:\python\模板.docx")
23     for table_col in range(0, sheet.ncols):
24         change_text(str(sheet.cell_value(0, table_col)), str(sheet.cell_value(table_row, table_col)))
25 
26     document.save("%s合同.docx" % str(sheet.cell_value(table_row, 0)))
27     print("%s合同完成" % str(sheet.cell_value(table_row, 0)))

 

 


 

批量生成不同合同(模块docx)

原文:https://www.cnblogs.com/zhouyxh/p/12317433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!