import xlrd
import time
import tkinter as tk
from tkinter import filedialog
from docx import Document
def info_update(doc, old_info, new_info):
for para in doc.paragraphs:
for run in para.runs:
run.text = run.text.replace(old_info, new_info)
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
cell.text = cell.text.replace(old_info, new_info)
print('''
注意:请确保花名册excel存在“在职员工”sheet,且标题行存在“姓名”“证件号”“当前合同起始时间”“岗位”
''')
print("="*30)
first = 0
while first == 0:
print("请选择花名册文件")
root = tk.Tk()
root.withdraw()
try:
Filepath = filedialog.askopenfilename()
print(Filepath)
workbook = xlrd.open_workbook(Filepath)
print("花名册读取成功...")
first = 1
except Exception as e:
print("出错:"+str(e))
print("一般报错都是你没有选择花名册文件哟")
time.sleep(5)
try:
sheet = workbook.sheet_by_name('在职员工')
row = sheet.row_values(0)
a = row.index('姓名')
b = row.index('证件号')
c = row.index('当前合同起始时间')
d = row.index('岗位')
except Exception as e:
print("出错:"+str(e))
print("一般报错都是标题行不符合要求哟,请关闭程序核查...")
time.sleep(200)
name = input('请输入姓名:')
for i in range(sheet.nrows):
if sheet.cell_value(i, a) == name:
e = i
break
print('证件号:', sheet.cell_value(e, b))
print(sheet.cell_value(e, c))
if type(sheet.cell_value(e, c)).__name__ == 'float':
dt_2 = xlrd.xldate_as_datetime(sheet.cell_value(e, c),0)
dt_1 = str(dt_2).split(" ")[0]
dt = "{}年{}月{}日".format(*dt_1.split("-"))
elif len(sheet.cell_value(e, c)) == 19 and ":" in sheet.cell_value(e, c):
dt_1 = sheet.cell_value(e, c).split(" ")[0]
dt = "{}年{}月{}日".format(*dt_1.split("-"))
else:
dt = sheet.cell_value(e, c)
print('当前合同起始时间:', dt)
print('岗位:', sheet.cell_value(e, d))
id_num = sheet.cell_value(e, b)
sex = eval(id_num[16:18])
if(sex%2==0):
gender ="男"
else:
gender ="女"
print('性别:', gender)
try:
doc = Document('./模板/在职证明(脚本模板).docx')
except Exception as e:
print("找不到./模板/在职证明(脚本模板).docx 文件,请关闭程序核查...")
time.sleep(200)
info_update(doc, '【姓名】', name)
info_update(doc, '【性别】', gender)
info_update(doc, '【证件号】', sheet.cell_value(e, b))
info_update(doc, '【当前合同起始时间】', dt)
info_update(doc, '【岗位】', sheet.cell_value(e, d))
print(f'生成 在职证明-{name}.docx')
doc.save(f'在职证明-{name}.docx')
print("="*30)
input("")在职证明(脚本模板).docx 内容
在职证明
兹证明 【姓名】为我司员工(性别: 【性别】,身份证号: 【证件号】),自 【当前合同起始时间】起至今在我司担任 【岗位】职务。
特此证明。
公司全称:XXX有限责任公司
Word设置定时更新时间
该脚本使用Python的xlrd库读取Excel花名册,docx库更新Word模板内容。用户输入姓名后,脚本找到对应员工信息,如性别、证件号、合同起始时间和岗位,并在模板中替换相应标签,生成在职证明。

被折叠的 条评论
为什么被折叠?



