在企业招聘中,最重要的事情,就是需要签订劳动合同,但是有些大的公司,因为人员过于,在设置编辑合同的时候,比较耗时耗力,编程存在的意义,就是帮助我们实现办公自动化,因此能实现自动生成合同,还是非常重要的,下面小编就来采用函数以及面向对象过程,教大家实现生成合同过程。
1、模板文件
甲方公司、乙方人员姓名、乙方部门、乙方职位、甲方人员姓名、乙方人员姓名、时间(年月日),如图所示。
2、实现代码
from docxtpl import DocxTemplate import os import pymysql import time cur_path = os.path.dirname(__file__) tempfilename = os.path.join(cur_path, 'template', '劳动合同模板.docx') today = time.strftime("%Y-%m-%d", time.localtime()) def query(): try: # 数据库连接,返回数据库连接对象 conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='test', port=3306) cur = conn.cursor() sql = 'select * from t_person_info' cur.execute(sql) result = cur.fetchall() return result except Exception as e: print(e) finally: conn.close() def build_hetong(): result = query() for x in result: tpl = DocxTemplate(tempfilename) context = { 'firstparty': '灯塔教育', 'secondparty': x[1], 'department': x[15], 'job': x[16], 'owner': '龙卷风', 'name': x[1], 'sj': today } tpl.render(context) savefilename=os.path.join(cur_path,'build',x[1]+'劳动合同.docx') tpl.save(savefilename) if __name__ == "__main__": start = time.time() build_hetong() end = time.time() sj = end-start print(f"花费时间(秒):{sj}")
3、输出结果
以上就是实现批量输出多个合同的实现内容了,感兴趣的小伙伴可以 *** 作起来试试哦~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)