sample_snapshot.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """示例:计算 青浦-北斗园区 2026-03 的月度快照字段。"""
  2. from __future__ import annotations
  3. import re
  4. from pathlib import Path
  5. import pandas as pd
  6. HR_ROOT = Path(r"E:\CODE\knowledge_agent\data\20260529_申勤提供数据\人事部")
  7. FIN = Path(r"E:\CODE\knowledge_agent\data\20260529_申勤提供数据\财务部\2026按合同物业收费进程26.5.17(1).xlsx")
  8. TARGET_PROJECT = "青浦-北斗园区"
  9. TARGET_MONTH = 3
  10. YEAR = 2026
  11. def _header_info(df: pd.DataFrame) -> tuple[str, int]:
  12. project, year = "", YEAR
  13. for r in range(min(3, len(df))):
  14. for c in range(min(12, df.shape[1])):
  15. v = df.iat[r, c]
  16. if pd.isna(v):
  17. continue
  18. s = str(v)
  19. m = re.search(r"项目[::]\s*(.+)", s)
  20. if m and not project:
  21. project = m.group(1).strip()
  22. ym = re.search(r"20(\d{2})\s*年", s)
  23. if ym:
  24. year = 2000 + int(ym.group(1))
  25. return project, year
  26. def _month_from_sheet(sh: str) -> int | None:
  27. m = re.search(r"(\d{1,2})\s*月", sh)
  28. return int(m.group(1)) if m else None
  29. def main() -> None:
  30. emp_ids: set[str] = set()
  31. att_rows = 0
  32. sums = {"平时加班小时": 0.0, "国定加班小时": 0.0, "餐费补助金额": 0.0,
  33. "值班费金额": 0.0, "加班超时费金额": 0.0, "国定加班费金额": 0.0}
  34. for p in sorted(HR_ROOT.rglob("*.xls")):
  35. try:
  36. xls = pd.ExcelFile(p)
  37. except Exception: # noqa: BLE001
  38. continue
  39. for sh in xls.sheet_names:
  40. month = _month_from_sheet(sh)
  41. if month != TARGET_MONTH:
  42. continue
  43. try:
  44. df = pd.read_excel(xls, sheet_name=sh, header=None, dtype=str)
  45. except Exception: # noqa: BLE001
  46. continue
  47. project, year = _header_info(df)
  48. if project != TARGET_PROJECT or year != YEAR:
  49. continue
  50. sub = df.iloc[4:].copy()
  51. sub.columns = range(sub.shape[1])
  52. sub = sub[sub[3].notna()]
  53. att_rows += len(sub)
  54. emp_ids.update(str(v).strip() for v in sub[1].dropna())
  55. for col, key in [(36, "平时加班小时"), (37, "国定加班小时"),
  56. (38, "餐费补助金额"), (39, "加班超时费金额"),
  57. (40, "国定加班费金额"), (41, "值班费金额")]:
  58. if sub.shape[1] > col:
  59. sums[key] += pd.to_numeric(sub[col], errors="coerce").sum()
  60. print(f"== {TARGET_PROJECT} {YEAR}年{TARGET_MONTH}月 考勤快照字段 ==")
  61. print(f"在岗人数: {len(emp_ids)}(当月有考勤记录的不同工号)")
  62. print(f"考勤人次: {att_rows}")
  63. for k, v in sums.items():
  64. print(f"{k}: {v:.2f}")
  65. # 财务:该项目 3 月物业费
  66. df = pd.read_excel(FIN, header=None, dtype=str)
  67. data = df.iloc[4:].copy()
  68. data.columns = range(data.shape[1])
  69. fin = data[data[1].astype(str).str.contains(TARGET_PROJECT, na=False)]
  70. print(f"\n== 财务(项目名含 '{TARGET_PROJECT}' 的合同行)==")
  71. if len(fin):
  72. amt = pd.to_numeric(fin[24], errors="coerce").sum() # 24 = 3月物业费列
  73. print(f"3月物业费(开票口径)合计: {amt:.2f}({len(fin)} 个合同行)")
  74. print("涉及合同:", fin[3].dropna().tolist())
  75. else:
  76. print("财务表中未找到该项目(需核对项目简称)")
  77. if __name__ == "__main__":
  78. main()