From bef29011c3f4156b4697ef11b9199d114573808f Mon Sep 17 00:00:00 2001 From: mxr612 Date: Thu, 12 Jun 2025 15:37:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E5=B7=A5=E9=BE=84?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=95=B4=E7=90=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=EF=BC=88=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=B8=8E=E5=A4=84=E7=90=86=E7=9A=84=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增多个工具函数以处理日期、整数转换和职级分割,增强数据处理的灵活性 - 更新工龄计算逻辑,确保准确反映员工的工龄和学龄 - 移除冗余代码,提升代码可读性和维护性 --- main.py | 83 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index f1f4517..9dfd77d 100644 --- a/main.py +++ b/main.py @@ -9,32 +9,75 @@ from datetime import datetime # 配置日志记录 logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') +# 常量 + P_LIMIT = 6 # 最大晋升次数 P_START = 10 # 晋升记录开始行 H_START = 15 + P_LIMIT # 历史记录开始行 +nowtime = datetime.now() + +# 工具函数 + # 自定义日期解析函数 def custom_date_parser(x): try: return datetime.strptime(x, '%Y-%m-%d') except: return x + +def format_time(dt,info): + try: + return dt.strftime("%Y.%m") + except: + logging.warning(f"[{info}]时间格式错误:{dt}") + return dt + +def to_int(x): + try: + return int(x) + except: + return 0 + +def fallback(x): + for i in x: + if pd.notna(i) and i != '': + return i + return '' + +def split_level(level:str): + try: + parts = level.split('-') + return (int(parts[0]), int(parts[1])) + except: + raise Exception(f"职级[{level}]格式错误") + +# 读取员工数据 BaseData = pd.read_excel("原数据.xlsx", sheet_name="入职信息") -Promote = pd.read_excel("原数据.xlsx", sheet_name="职务变动") # for col in ["出生年月","任职年月","原职时间","参加工作时间","入职时间", "晋档起始", "晋级起始", "日期2"]: BaseData[col] = BaseData[col].apply(custom_date_parser) - for col in ["晋档起始", "晋级起始"]: BaseData[col] = BaseData[col].apply(lambda x: datetime(x.year, 1, 1) if isinstance(x, datetime) else x) +BaseData["工龄调增"] = BaseData["工龄调增"].apply(to_int) +BaseData["工龄调减"] = BaseData["工龄调减"].apply(to_int) +BaseData["学龄"] = BaseData["学龄"].apply(to_int) +BaseData["工龄"] = BaseData.apply(lambda row: nowtime.year-row["参加工作时间"].year+row["工龄调增"]-row["工龄调减"]+1, axis=1) + +# 读取晋升记录 + +Promote = pd.read_excel("原数据.xlsx", sheet_name="职务变动") # + for col in ["任职时间","工资执行时间"]: Promote[col] = Promote[col].apply(custom_date_parser) logging.info("人员信息加载完成") +# 读取规则 + Rule_Role = [] col = 4 while True: # 职位规则 @@ -94,16 +137,6 @@ Rule_Role = sorted(Rule_Role, key=lambda x: x['start']) Rule_Level = sorted(Rule_Level, key=lambda x: x['start']) Rule_RoleName = sorted(Rule_RoleName, key=lambda x: x['start']) -nowtime = datetime.now() - -def split_level(level:str): - try: - parts = level.split('-') - return (int(parts[0]), int(parts[1])) - except: - raise Exception(f"职级[{level}]格式错误") - return (0, 0) - def role_salary(role:str, time): for rule in Rule_Role: if rule["start"] <= time <= rule["end"]: @@ -134,28 +167,6 @@ def role_limit(role:str): logging.warning(f"职位[{role}]不存在职级上限规则") return -1 -def format_time(dt,info): - try: - return dt.strftime("%Y.%m") - except: - logging.warning(f"[{info}]时间格式错误:{dt}") - return dt - -def to_int(x): - try: - return int(x) - except: - return 0 -BaseData["工龄调增"] = BaseData["工龄调增"].apply(to_int) -BaseData["工龄调减"] = BaseData["工龄调减"].apply(to_int) -BaseData["学龄"] = BaseData["学龄"].apply(to_int) - -def fallback(x): - for i in x: - if pd.notna(i) and i != '': - return i - return '' - max_promote = 0 max_history = 0 @@ -343,8 +354,8 @@ for index, row in BaseData.iterrows(): # 汇总 ws.cell(row=6+index, column=4, value=format_time(row["出生年月"], "出生年月")) ws.cell(row=6+index, column=5, value=format_time(row["参加工作时间"], "参加工作时间")) ws.cell(row=6+index, column=6, value=fallback([row["现学历"],row["学历"]])) - ws.cell(row=6+index, column=7, value=nowtime.year-row["参加工作时间"].year+row["工龄调增"]-row["工龄调减"]+1+row["学龄"]) - ws.cell(row=6+index, column=8, value=nowtime.year-row["参加工作时间"].year+row["工龄调增"]-row["工龄调减"]+1) + ws.cell(row=6+index, column=7, value=row['工龄']+row["学龄"]) + ws.cell(row=6+index, column=8, value=row['工龄']) ws.cell(row=6+index, column=9, value=row["学龄"]) ws.cell(row=6+index, column=10, value=row["工龄调减"]) ws.cell(row=6+index, column=11, value=row["Latest_Role"])