当前位置:   article > 正文

人事工作中的Python运用——离职证明生成器

离职证明生成

人力资源管理工作的其他Python运用

人事工作中的Python运用——批量劳动合同+实习协议_派圣的博客-CSDN博客

相信大家工作中,会时常有员工向我们所要离职证明或者实习协议,遇见我们人事工作手头很紧,忙不过来,那么自动生成神奇你值得拥有,其实逻辑跟上面那个文章差不多,大家看一下就懂了,就可以发挥各种想象去开发了!

脚本逻辑:

1.我们公司会有自己制作的花名册excel,里面有很多sheet,有个“离职”sheet

2.请确认列数对应与脚本一致,不一致修改即可

准备模板文件(命名:“离职证明【模板勿动】.docx”),请务必替换字符要带下划线;

XXX有限公司

离职证明

     兹有,身份证号码为  懿  ,于与我司签订劳动合同,离职时工作岗位为  鰘   。   

经协商一致,我司同意该员工于日解除劳动合同。

   

特此证明。

本人签字:                             

XXX有限公司

                                                   

 代码一(无UI):

  1. from docx import Document
  2. from openpyxl import load_workbook
  3. import os
  4. import datetime
  5. import time
  6. # 结合路径判断生成文件夹,规避程序报错而终止的风险
  7. if not os.path.exists('./' + '离职证明导出'):
  8. os.mkdir('./' + '离职证明导出')
  9. print("创建目录成功")
  10. import tkinter as tk
  11. from tkinter import filedialog
  12. print("请选择花名册文件")
  13. try:
  14. root = tk.Tk()
  15. root.withdraw()
  16. Filepath = filedialog.askopenfilename()
  17. workbook = load_workbook(Filepath)
  18. except:
  19. print("未选择文件,请重新选择!")
  20. root = tk.Tk()
  21. root.withdraw()
  22. Filepath = filedialog.askopenfilename()
  23. workbook = load_workbook(Filepath)
  24. sheet = workbook["离职"]
  25. numbers = int(sheet.max_row)-1
  26. who = input("请输入姓名:")
  27. summit = 0
  28. for table_row in range(2, sheet.max_row + 1):
  29. name = str(sheet.cell(row=table_row, column=6).value)
  30. if name == who:
  31. print("找到1个符合条件")
  32. try:
  33. try:
  34. print("=====入职日期=====")
  35. date_kaishi = sheet['I{}'.format(table_row)].value
  36. try:
  37. date_kaishi_year = "20"+str(date_kaishi.strftime('%y'))
  38. date_kaishi_month = str(date_kaishi.strftime('%m'))
  39. date_kaishi_day = str(date_kaishi.strftime('%d'))
  40. except:
  41. date_kaishi_year = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').year
  42. date_kaishi_month = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').month
  43. date_kaishi_day = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').day
  44. print(date_kaishi_year)
  45. print(date_kaishi_month)
  46. print(date_kaishi_day)
  47. print("=====离职日期=====")
  48. date_zhongzhi = sheet['J{}'.format(table_row)].value
  49. try:
  50. date_zhongzhi_year = "20"+str(date_zhongzhi.strftime('%y'))
  51. date_zhongzhi_month = str(date_zhongzhi.strftime('%m'))
  52. date_zhongzhi_day = str(date_zhongzhi.strftime('%d'))
  53. except:
  54. date_zhongzhi_year = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').year
  55. date_zhongzhi_month = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').month
  56. date_zhongzhi_day = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').day
  57. print(date_zhongzhi_year)
  58. print(date_zhongzhi_month)
  59. print(date_zhongzhi_day)
  60. print("=====身份证=====")
  61. date_ID = sheet['H{}'.format(table_row)].value
  62. print(date_ID)
  63. print("=====岗位=====")
  64. GW = sheet['E{}'.format(table_row)].value
  65. print(GW)
  66. wordfile = Document('./' + '离职证明【模板勿动】.docx')
  67. all_paragraphs = wordfile.paragraphs
  68. for paragraph in all_paragraphs:
  69. for run in paragraph.runs:
  70. try:
  71. if "奰" in run.text:
  72. run.text = run.text.replace("奰", date_kaishi_year)
  73. elif "躄" in run.text:
  74. run.text = run.text.replace("躄", date_kaishi_month)
  75. elif "罍" in run.text:
  76. run.text = run.text.replace("罍", date_kaishi_day)
  77. elif "颣" in run.text:
  78. run.text = run.text.replace("颣", date_zhongzhi_year)
  79. elif "薐" in run.text:
  80. run.text = run.text.replace("薐", date_zhongzhi_month)
  81. elif "豳" in run.text:
  82. run.text = run.text.replace("豳", date_zhongzhi_day)
  83. elif "懿" in run.text:
  84. run.text = run.text.replace("懿", str(date_ID))
  85. elif "鰘" in run.text:
  86. run.text = run.text.replace("鰘", str(GW))
  87. elif "礥" in run.text:
  88. run.text = run.text.replace("礥", str(who))
  89. except Exception as e:
  90. print("替换文本出错:"+str(e))
  91. wordfile.save('./' + f'离职证明导出/{table_row}_{name}_离职证明.docx')
  92. print(f"{table_row}_{name}_离职证明.docx | 另存成功")
  93. summit += 1
  94. except Exception as e:
  95. print("内出错:"+str(e))
  96. except Exception as e:
  97. print("外出错:"+str(e))
  98. if summit == 0:
  99. print(f"未查到 {who}!!!!!\n")
  100. elif summit == 1:
  101. print("导出一份,结束!\n")
  102. elif summit >= 2:
  103. print("结束!\n")
  104. print(f"本次共导出 {summit}{who}的离职文件,请注意筛选!!!!!\n")
  105. input ("Please Enter to close this exe:")

 代码一(有UI,用的wxpython框架):

  1. # -*- coding: utf-8 -*-
  2. import wx
  3. import time
  4. import tkinter as tk
  5. from tkinter import filedialog
  6. from docx import Document
  7. from openpyxl import load_workbook
  8. import os
  9. import datetime
  10. def lzzm(Filepath,who):
  11. if not os.path.exists('./' + '离职证明导出'):
  12. os.mkdir('./' + '离职证明导出')
  13. contents.AppendText("创建目录成功\n")
  14. workbook = load_workbook(Filepath)
  15. sheet = workbook["离职"]
  16. summit = 0
  17. for table_row in range(2, sheet.max_row + 1):
  18. name = str(sheet.cell(row=table_row, column=6).value)
  19. if name == who:
  20. contents.AppendText("查到一个符合条件\n")
  21. try:
  22. try:
  23. contents.AppendText("=====入职日期=====\n")
  24. date_kaishi = sheet['I{}'.format(table_row)].value
  25. try:
  26. date_kaishi_year = "20"+str(date_kaishi.strftime('%y'))
  27. date_kaishi_month = str(date_kaishi.strftime('%m'))
  28. date_kaishi_day = str(date_kaishi.strftime('%d'))
  29. except:
  30. date_kaishi_year = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').year
  31. date_kaishi_month = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').month
  32. date_kaishi_day = datetime.datetime.strptime(date_kaishi,'%Y-%m-%d').day
  33. contents.AppendText(date_kaishi_year)
  34. contents.AppendText(date_kaishi_month)
  35. contents.AppendText(date_kaishi_day+"\n")
  36. contents.AppendText("=====离职日期=====\n")
  37. date_zhongzhi = sheet['J{}'.format(table_row)].value
  38. try:
  39. date_zhongzhi_year = "20"+str(date_zhongzhi.strftime('%y'))
  40. date_zhongzhi_month = str(date_zhongzhi.strftime('%m'))
  41. date_zhongzhi_day = str(date_zhongzhi.strftime('%d'))
  42. except:
  43. date_zhongzhi_year = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').year
  44. date_zhongzhi_month = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').month
  45. date_zhongzhi_day = datetime.datetime.strptime(date_zhongzhi,'%Y-%m-%d').day
  46. contents.AppendText(date_zhongzhi_year)
  47. contents.AppendText(date_zhongzhi_month)
  48. contents.AppendText(date_zhongzhi_day+"\n")
  49. contents.AppendText("=====身份证=====\n")
  50. date_ID = sheet['H{}'.format(table_row)].value
  51. contents.AppendText(str(date_ID)+"\n")
  52. contents.AppendText("=====岗位=====\n")
  53. GW = sheet['E{}'.format(table_row)].value
  54. contents.AppendText(str(GW)+"\n")
  55. contents.AppendText("即将生成文件...\n")
  56. wordfile = Document('./' + '离职证明【模板勿动】.docx')
  57. all_paragraphs = wordfile.paragraphs
  58. for paragraph in all_paragraphs:
  59. for run in paragraph.runs:
  60. try:
  61. if "奰" in run.text:
  62. run.text = run.text.replace("奰", date_kaishi_year)
  63. elif "躄" in run.text:
  64. run.text = run.text.replace("躄", date_kaishi_month)
  65. elif "罍" in run.text:
  66. run.text = run.text.replace("罍", date_kaishi_day)
  67. elif "颣" in run.text:
  68. run.text = run.text.replace("颣", date_zhongzhi_year)
  69. elif "薐" in run.text:
  70. run.text = run.text.replace("薐", date_zhongzhi_month)
  71. elif "豳" in run.text:
  72. run.text = run.text.replace("豳", date_zhongzhi_day)
  73. elif "懿" in run.text:
  74. run.text = run.text.replace("懿", str(date_ID))
  75. elif "鰘" in run.text:
  76. run.text = run.text.replace("鰘", str(GW))
  77. elif "礥" in run.text:
  78. run.text = run.text.replace("礥", str(who))
  79. except Exception as e:
  80. print("替换文本出错:"+str(e))
  81. wordfile.save('./' + f'离职证明导出/{table_row}_{name}_离职证明.docx')
  82. contents.AppendText(f"{table_row}_{name}_离职证明.docx | 另存成功\n")
  83. summit += 1
  84. except Exception as e:
  85. contents.AppendText("内出错:"+str(e)+"\n")
  86. except Exception as e:
  87. contents.AppendText("外出错:"+str(e)+"\n")
  88. if summit == 0:
  89. contents.AppendText(f"未查到 {who}!!!!!\n")
  90. elif summit == 1:
  91. contents.AppendText("导出一份,结束!\n")
  92. elif summit >= 2:
  93. contents.AppendText("结束!\n")
  94. contents.AppendText(f"本次共导出 {summit}{who}的离职文件,请注意筛选!!!!!\n")
  95. def choice(event):
  96. print("请选择文件")
  97. root = tk.Tk()
  98. root.withdraw()
  99. Filepath = filedialog.askopenfilename() #获得选择好的文件
  100. print('Filepath:',Filepath)
  101. filename.SetValue(Filepath)
  102. def Go(event):
  103. if filename.GetValue() == "" or filename.GetValue() == "未选择文件":
  104. contents.AppendText("未选择文件,请选择后运行\n")
  105. elif filename1.GetValue() == "请输入姓名" or filename1.GetValue() == "":
  106. contents.AppendText("未输入姓名,请输入后运行\n")
  107. else:
  108. try:
  109. lzzm(Filepath=filename.GetValue(),who=filename1.GetValue())
  110. except Exception as e:
  111. contents.AppendText(f"{e}:运行失败!\n")
  112. app = wx.App()
  113. win = wx.Frame(None,title = "离职证明快速生成小助手", size=(535,520))
  114. bkg = wx.Panel(win)
  115. #设置icon
  116. ##icon = wx.Icon(r'logo.ico')
  117. ##win.SetIcon(icon)
  118. #设置透明度
  119. win.SetTransparent(230)
  120. loadButton = wx.Button(bkg, label = '选择文件')
  121. loadButton.Bind(wx.EVT_BUTTON,choice)
  122. saveButton = wx.Button(bkg, label = '开始运行')
  123. saveButton.Bind(wx.EVT_BUTTON,Go)
  124. filename = wx.TextCtrl(bkg,value = "未选择文件", style = wx.TE_READONLY)
  125. filename1 = wx.TextCtrl(bkg,value = "请输入姓名")
  126. contents = wx.TextCtrl(bkg,value = "程序指南:\n1.选择文件选择花名册后\n2.请输入需要制作离职证明的姓名\n3.点击开始运行,如果在离职的sheet里面存在此人会导出,反之不存在!\n ====================\n", style = wx.TE_MULTILINE | wx.HSCROLL | wx.TE_READONLY)
  127. hbox = wx.BoxSizer()
  128. hbox.Add(filename, proportion =1, flag = wx.EXPAND)
  129. hbox.Add(loadButton, proportion =0,flag = wx.LEFT, border = 5)
  130. pbox = wx.BoxSizer()
  131. pbox.Add(filename1, proportion =1, flag = wx.EXPAND)
  132. pbox.Add(saveButton, proportion =0,flag = wx.LEFT, border = 5)
  133. vbox = wx.BoxSizer(wx.VERTICAL)
  134. vbox.Add(hbox,proportion = 0,flag = wx.EXPAND | wx.ALL, border = 5)
  135. vbox.Add(pbox,proportion = 0,flag = wx.EXPAND | wx.ALL, border = 5)
  136. vbox.Add(contents, proportion = 1,flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border = 5)
  137. bkg.SetSizer(vbox)
  138. win.Show()
  139. app.MainLoop()

测试:

2023年2月27如升级版:自动根据标题处理

  1. import xlrd
  2. import time
  3. import tkinter as tk
  4. from tkinter import filedialog
  5. from docx import Document
  6. def info_update(doc, old_info, new_info):
  7. for para in doc.paragraphs:
  8. for run in para.runs:
  9. run.text = run.text.replace(old_info, new_info)
  10. for table in doc.tables:
  11. for row in table.rows:
  12. for cell in row.cells:
  13. cell.text = cell.text.replace(old_info, new_info)
  14. print('''
  15. 注意:请确保花名册excel存在“离职”sheet,且标题行存在“姓名”“岗位”“身份证号”“入职日期”“离职日期”
  16. ''')
  17. print("="*30)
  18. first = 0
  19. while first == 0:
  20. print("请选择花名册文件")
  21. root = tk.Tk()
  22. root.withdraw()
  23. try:
  24. Filepath = filedialog.askopenfilename()
  25. print(Filepath)
  26. workbook = xlrd.open_workbook(Filepath)
  27. print("花名册读取成功...")
  28. first = 1
  29. except Exception as e:
  30. print("出错:"+str(e))
  31. print("一般报错都是你没有选择花名册文件哟")
  32. time.sleep(5)
  33. try:
  34. sheet = workbook.sheet_by_name('离职')
  35. row = sheet.row_values(0)
  36. a = row.index('姓名')
  37. b = row.index('身份证号')
  38. c = row.index('入职日期')
  39. d = row.index('岗位')
  40. f = row.index('离职日期')
  41. except Exception as e:
  42. print("出错:"+str(e))
  43. print("一般报错都是标题行不符合要求哟,请关闭程序核查...")
  44. time.sleep(200)
  45. e = 0
  46. name = input('请输入姓名:')
  47. for i in range(sheet.nrows):
  48. if sheet.cell_value(i, a) == name:
  49. e = i
  50. break
  51. if e == 0:
  52. print(f"在离职sheet里面未找到 {name},脚本停止运行...")
  53. time.sleep(200)
  54. else:
  55. print(name)
  56. print('身份证号:', sheet.cell_value(e, b))
  57. #入职日期
  58. print(sheet.cell_value(e, c))
  59. if type(sheet.cell_value(e, c)).__name__ == 'float':
  60. rz_2 = xlrd.xldate_as_datetime(sheet.cell_value(e, c),0)
  61. rz_1 = str(rz_2).split(" ")[0]
  62. rz = "{}年{}月{}日".format(*rz_1.split("-"))
  63. elif len(sheet.cell_value(e, c)) == 19 and ":" in sheet.cell_value(e, c):
  64. rz_1 = sheet.cell_value(e, c).split(" ")[0]
  65. rz = "{}年{}月{}日".format(*rz_1.split("-"))
  66. else:
  67. rz = sheet.cell_value(e, c)
  68. print('入职日期:', rz)
  69. #离职日期
  70. print(sheet.cell_value(e, f))
  71. if type(sheet.cell_value(e, f)).__name__ == 'float':
  72. lz_2 = xlrd.xldate_as_datetime(sheet.cell_value(e, f),0)
  73. lz_1 = str(lz_2).split(" ")[0]
  74. lz = "{}年{}月{}日".format(*lz_1.split("-"))
  75. elif len(sheet.cell_value(e, f)) == 19 and ":" in sheet.cell_value(e, f):
  76. lz_1 = sheet.cell_value(e, f).split(" ")[0]
  77. lz = "{}年{}月{}日".format(*lz_1.split("-"))
  78. else:
  79. lz = sheet.cell_value(e, f)
  80. print('离职日期:', lz)
  81. print('岗位:', sheet.cell_value(e, d))
  82. id_num = sheet.cell_value(e, b)
  83. sex = eval(id_num[16:18])
  84. if(sex%2==0):
  85. gender ="男"
  86. else:
  87. gender ="女"
  88. print('性别:', gender)
  89. try:
  90. doc = Document('./模板/离职证明(脚本模板).docx')
  91. except Exception as e:
  92. print("找不到./模板/离职h证明(脚本模板).docx 文件,请关闭程序核查...")
  93. time.sleep(200)
  94. info_update(doc, '【姓名】', name)
  95. info_update(doc, '【性别】', gender)
  96. info_update(doc, '【身份证号】', sheet.cell_value(e, b))
  97. info_update(doc, '【入职日期】', rz)
  98. info_update(doc, '【离职日期】', rz)
  99. info_update(doc, '【岗位】', sheet.cell_value(e, d))
  100. print(f'生成 离职证明-{name}.docx')
  101. doc.save(f'离职证明-{name}.docx')
  102. print("="*30)
  103. input("")

离职证明(脚本模板).docx 内容

离职证明

兹证明 【姓名】 (性别 【性别】 ,身份证号 【身份证号】 ) 【入职日期】  【离职日期】 在我司担任 【岗位】 职务,在此工作期间无不良表现,工作良好,同事关系融洽。经个人申请公司研究决定同意其离职,已办理离职手续。

特此证明!

XXX有限责任公司

2023年2月27日

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/741354
推荐阅读
相关标签
  

闽ICP备14008679号