生成证明模版文件
一直以来通过word写证明,老是需要查询用户身份证号码等,感觉很麻烦,突然有想法,想把查询的是交给数据库,然后通过模版来替换,
减少工作量,接下来是实现的一些代码,希望对你有帮助。
import re
'''
Function:写证明时候通过查询数据库,根据相应的模版,生成证明文件
Author: kennyhip
Date: 2018年9月16日
'''
def prod_file_by_txt(file_path,dic):
'''
模版替换
file_path 传入的文件
dic 字典
'''
context = ''
with open(file_path,'r') as f:
for line in f.read(512):
context += line
for k,v in dic.items():
context = re.sub('zb',dic['zb'],context)
context = re.sub('name',dic['name'],context)
context = re.sub('sex',dic['sex'],context)
context = re.sub('mz',dic['mz'],context)
context = re.sub('sfzh',dic['sfzh'],context)
context = re.sub('deathtime',dic['deathtime'],context)
context = re.sub('curtime',dic['curtime'],context)
return context
if __name__ == '__main__':
dic = {'zb':'二组','name':'张三','sex':'男','mz':'汉族','sfzh':'532924200210102321','deathtime':'2018年06月15日','curtime':'2018年09月16日'}
print(prod_file_by_txt('户口册遗失证明.txt',dic))

浙公网安备 33010602011771号