irafa.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. # @Author: privacy
  3. # @Date: 2022-07-07 13:12:17
  4. # @Last Modified by: privacy
  5. # @Last Modified time: 2022-07-16 09:08:32
  6. # 内部人才市场简历模板
  7. from pprint import pprint
  8. import re
  9. import json
  10. import docx
  11. from docx import Document
  12. from docx.shared import Inches
  13. path = "d:\\desktop\\内部人才市场简历模板.docx"
  14. keywords = ["姓名", "性别", "出生日期", "民族", "籍贯", "健康状况", "政治面貌", "参加工作时间", "外语水平", "专业技术资格(取得时间)", "计算机水平", "熟悉专业有何专长", "工作单位", "现任职务", "任职时间", "联系电话", "对报名岗位认识及工作设想", "意向地区", "意向岗位", "意向单位", "意向专业", "职业证书", "资格等级", "取得日期", "学校/培训机构", "专业", "起始时间", "毕业时间", "姓名", "职业", "与本人关系"]
  15. def parse_line(line):
  16. result = []
  17. key = None
  18. for cell in line:
  19. if cell and ''.join(cell.split()) in keywords:
  20. key = ''.join(cell.split())
  21. elif cell and key:
  22. schema = {key:cell}
  23. result.append(schema)
  24. key = None
  25. return result
  26. def parse_layout(path):
  27. result = []
  28. doc = Document(path)
  29. lo = {}
  30. tables = doc.tables
  31. for _table in tables[:]:
  32. for i, row in enumerate(_table.rows[:]):
  33. row_content = []
  34. for cell in row.cells[:]:
  35. c = cell.text
  36. row_content.append(c)
  37. lo[len(lo.keys())] = row_content
  38. kwln = -1
  39. kwline = None
  40. for key in lo.keys():
  41. # pdb.set_trace()
  42. for val in lo[key]:# 通过全关键词,判断此行是否为关键词行
  43. if val and ''.join(val.split()) not in keywords:# 有非关键字元素,非关键词行,判断是否为关键词行元素
  44. # pdb.set_trace()
  45. for c in lo[key]:
  46. # pdb.set_trace()
  47. if c and ''.join(c.split()) in keywords:# 非关键词行元素
  48. result.extend(parse_line(lo[key]))
  49. break
  50. else:# 关键词行元素
  51. schema = dict()
  52. for key, val in zip(kwline, lo[key]):
  53. if key:
  54. schema[key] = val
  55. if "学校/培训机构" in schema.keys():
  56. schema["学习经历"] = "学习经历"
  57. elif "与本人关系" in schema.keys():
  58. schema["家庭成员"] = "家庭成员"
  59. elif "意向地区" in schema.keys():
  60. schema["职业发展管理"] = "职业发展管理"
  61. elif "职业证书" in schema.keys():
  62. schema["职业资格证书"] = "职业资格证书"
  63. result.append(schema)
  64. break
  65. break
  66. else:
  67. # print("此行为关键词行")
  68. kwline = [''.join(cell.split()) for cell in lo[key]]
  69. kwln = len(lo[key])
  70. job = {"工作经历":"工作经历"}
  71. flag = None
  72. for p in doc.paragraphs:
  73. text = p.text.replace(":", ":")
  74. if ":" in text:
  75. text = re.sub(r'(\w+)\W{0,2}:', r'\n\1:', text)
  76. for line in text.split("\n"):
  77. if line.strip():
  78. i = line.split(":")
  79. if job.get(i[0].strip()):
  80. result.append(job)
  81. job = {"工作经历":"工作经历"}
  82. job[i[0].strip()] = i[1].strip()
  83. flag = i[0].strip()
  84. elif flag == "工作描述":
  85. job["工作描述"] += '\n' + text.strip()
  86. else:
  87. result.append(job)
  88. return result
  89. # 格式化数据
  90. def formatter(datalist):
  91. result = dict()
  92. for d in datalist:
  93. if len(d) == 1:
  94. for key in d.keys():
  95. result[key] = d[key]
  96. else:
  97. for k in list(d.keys()):
  98. if k == "".join(d[k].split()):
  99. d.pop(k)
  100. if result.get(k):
  101. result[k].append(d)
  102. else:
  103. result[k] = [d]
  104. # 转译数据库字段名
  105. with open("./resources/translate.json", "r", encoding="utf-8") as ff:
  106. json_obj = json.load(ff)
  107. normal = json_obj["base"]
  108. itenormal = json_obj["base"]
  109. edunormal = json_obj["tal_training_institutions"]
  110. jobnormal = json_obj["tal_his_job"]
  111. cetnormal = json_obj["tal_vocational_qualification_certificate"]
  112. family = json_obj["tal_family_social_relations"]
  113. for key in normal.keys():
  114. if result.get(key):
  115. result[normal[key]] = result[key]
  116. result.pop(key)
  117. for idx in range(len(result['职业发展管理'])):
  118. for key in itenormal.keys():
  119. if result['职业发展管理'][idx].get(key):
  120. result['职业发展管理'][idx][itenormal[key]] = result['职业发展管理'][idx][key]
  121. result['职业发展管理'][idx].pop(key)
  122. for idx in range(len(result['学习经历'])):
  123. for key in edunormal.keys():
  124. if result['学习经历'][idx].get(key):
  125. result['学习经历'][idx][edunormal[key]] = result['学习经历'][idx][key]
  126. result['学习经历'][idx].pop(key)
  127. for idx in range(len(result['工作经历'])):
  128. for key in jobnormal.keys():
  129. if result['工作经历'][idx].get(key):
  130. result['工作经历'][idx][jobnormal[key]] = result['工作经历'][idx][key]
  131. result['工作经历'][idx].pop(key)
  132. for idx in range(len(result['职业资格证书'])):
  133. for key in cetnormal.keys():
  134. if result['职业资格证书'][idx].get(key):
  135. result['职业资格证书'][idx][cetnormal[key]] = result['职业资格证书'][idx][key]
  136. result['职业资格证书'][idx].pop(key)
  137. for idx in range(len(result['家庭成员'])):
  138. for key in family.keys():
  139. if result['家庭成员'][idx].get(key):
  140. result['家庭成员'][idx][family[key]] = result['家庭成员'][idx][key]
  141. result['家庭成员'][idx].pop(key)
  142. tit = {
  143. "基本信息":"base",
  144. "职业发展管理":"intent_job",
  145. "学习经历":"tal_training_institutions",
  146. "工作经历":"tal_his_job",
  147. "项目经历":"tal_his_project",
  148. "培训经历":"tal_training_institutions",
  149. "获奖情况":"tal_rewards_punishments",
  150. "语言能力":"tal_language",
  151. "职业资格证书":"tal_vocational_qualification_certificate",
  152. "专业技能":"tal_professional_tech_certificate",
  153. "家庭成员":"tal_family_social_relations"
  154. }
  155. for key in tit.keys():
  156. if result.get(key):
  157. result[tit[key]] = result[key]
  158. result.pop(key)
  159. # url = "http://192.168.1.110:9999/talent/getResumeData"
  160. # session = requests.Session()
  161. # session.mount('http://', HTTPAdapter(max_retries = 3))
  162. # try:
  163. # headers = {
  164. # 'contentType':'Application/json'
  165. # }
  166. # response = session.post(url=url, headers=headers, json={"ResumeData":result}, timeout=10)
  167. # print(response.text)
  168. # except Exception as e:
  169. # print(e)
  170. return result
  171. if __name__ == "__main__":
  172. pprint(formatter(parse_layout(path)))