批量生成合同

在工作中,可能需要做很多不复杂的合同,即修改合同中的多个合同元素,但是必须逐个打开合同以查找更改。 当合同太多或手头上有其他事物纠缠时,可能会不小心漏掉,忘记填写和修改。在这里可以用python一键解决你的困扰。大大提高工作效率。

生成Word文档版本合同 

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import Pt,Inches

#1.创建一个文档
document=Document()
style=document.styles['Normal']
style.font.name='宋体'
style.element.rPr.rFonts.set(qn('w:eastAsia'),'宋体')

#2.创建段落
#标题段落
title=document.add_paragraph('汽车贷款合同')
title.alignment=WD_PARAGRAPH_ALIGNMENT.CENTER
title.paragraph_format.space_after=Pt(20)
title.style.font.size=Pt(14)
#内容
content=document.add_paragraph()
content.paragraph_format.first_line_indent=Pt(20)
partA=content.add_run('itcast01')   #占位符 甲方
content.add_run('(甲方)于')
sign_date=content.add_run('itcast02') #占位符 时间
content.add_run('与')
partB=content.add_run('itcast03')    #占位符 乙方
content.add_run('(乙方)签订汽车贷款合同。乙方需要在')
day=content.add_run('itcast04')     #占位符 支付时间
content.add_run('天内向甲方支付人民币')
total_money=content.add_run('itcast05') #占位符 贷款金额
content.add_run('元来支持甲方购买汽车。甲方需要分为')
times=content.add_run('itcast06')       #占位符 分期数
content.add_run('期来偿还乙方本金。经过友好协商,甲方需要向乙方一次性支付')
fee=content.add_run('itcast07')         #占位符 服务费
content.add_run('元作为汽车贷款金融服务费。')

#署名(签字)
bottom1=document.add_paragraph('甲方:')
bottom1.alignment=WD_PARAGRAPH_ALIGNMENT.LEFT
bottom1.paragraph_format.left_indent=Inches(4)
bottom2=document.add_paragraph('乙方:')
bottom2.alignment=WD_PARAGRAPH_ALIGNMENT.LEFT
bottom2.paragraph_format.left_indent=Inches(4)
bottom3=document.add_paragraph('日期:    年  月  日')
bottom3.alignment=WD_PARAGRAPH_ALIGNMENT.LEFT
bottom3.paragraph_format.left_indent=Inches(4)
#保存文档
document.save('template.docx')

生成的合同template.docx

一键批量生成合同

from openpyxl import load_workbook
from docx import Document
from os import listdir
'''
定义替换函数
'''
def replace_text(old_text, new_text):
    all_paragraphs = document.paragraphs #读取所有的自然段
    for paragraph in all_paragraphs:
        for run in paragraph.runs: #循环读取所有的run,并进行新旧文本的替换
            run_text = run.text.replace(old_text, new_text)
            run.text = run_text
    all_tables = document.tables #读取所有的表格
    for table in all_tables:
        for row in table.rows:
            for cell in row.cells: #循环读取表格中所有的cells,并进行新旧文本的替换
                cell_text = cell.text.replace(old_text, new_text)
                cell.text = cell_text
'''
获取Excel和Word的文件
'''
for file in listdir():
    if '.xlsx' in file:
        xlsx_name = file
    if '.docx' in file:
        docx_name = file
'''
读取Excel内数据
'''
wb = load_workbook(xlsx_name)
sheetx0 = wb.sheetnames
sheetx = wb[sheetx0[0]]

'''
循环读取并替换
'''
for col in range(2,sheetx.max_column+1): #合同要素Excel中逐列循环
    document = Document(docx_name)
    if sheetx.cell(row=1,column=col).value!=None: #openpyxl在使用sheetx.max_column时可能会读取到空的单元格,这里进行剔除
        for l in range(1,sheetx.max_row+1): #合同要素Excel中逐行循环
            old_text = sheetx.cell(row=l,column=1).value #合同要素Excel中对第一列逐行读取编号
            new_text = sheetx.cell(row=l,column=col).value #合同要素Excel中对循环的当前列逐行读取新要素
            replace_text(str(old_text),str(new_text)) #进行替换
            filename = str(sheetx.cell(row=1,column=col).value) #定义文件名为当前列第一行的内容
        document.save("%s.docx"%(filename)) #按定义的文件名进行保存
print('合同生成完毕!')

实现效能:把我们需要生成的数据填写在Excel相应部分,然后将自动生成相对应版本的word合同。

生成的合同:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘 源 园

你的鼓励将是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值