111 lines
3.2 KiB
Python
111 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
scripts/fill_overtime_form.py
|
||
|
||
用途:按模板批量生成指定日期区间的加班申请单 .xls。
|
||
依赖:xlrd、xlutils.copy、xlwt。
|
||
逻辑:读取模板 ABCD 列固定信息;工作日在前、周六在后,跳过周日。
|
||
"""
|
||
|
||
from datetime import date, timedelta
|
||
|
||
import xlrd
|
||
from xlutils.copy import copy
|
||
|
||
SRC = r"E:\和信\加班\加班申请单模板_202601_原.xls"
|
||
OUT = r"E:\和信\加班\加班申请单_20260821-20260920.xls"
|
||
|
||
|
||
def _date_str(d: date) -> str:
|
||
"""将 date 格式化为模板使用的 YYYY-MM-DD 字符串。"""
|
||
return d.strftime("%Y-%m-%d")
|
||
|
||
|
||
def _overtime_hours(is_weekday: bool) -> str:
|
||
"""
|
||
按时间段计算加班时数。
|
||
核心逻辑:工作日 18:00–23:30 为 5.5 小时;周六 08:00–20:00 扣午休后为 10 小时。
|
||
"""
|
||
return "5.5" if is_weekday else "10"
|
||
|
||
|
||
def build_ordered_dates(start: date, end: date) -> tuple[list[date], list[date], list[date]]:
|
||
"""
|
||
遍历日期区间:工作日(周一~周五)在前,周六在后,跳过周日。
|
||
|
||
Returns:
|
||
(ordered_dates, weekdays, saturdays)
|
||
"""
|
||
weekdays: list[date] = []
|
||
saturdays: list[date] = []
|
||
current = start
|
||
while current <= end:
|
||
wd = current.weekday()
|
||
if wd < 5:
|
||
weekdays.append(current)
|
||
elif wd == 5:
|
||
saturdays.append(current)
|
||
# 周日 (wd == 6) 跳过
|
||
current += timedelta(days=1)
|
||
return weekdays + saturdays, weekdays, saturdays
|
||
|
||
|
||
def fill_overtime_workbook(src_path: str, out_path: str, start: date, end: date) -> None:
|
||
"""
|
||
复制模板并填入加班明细。
|
||
核心逻辑:保留表头与 ABCD;工作日类型 026 / 18:00–23:30,周六类型 027 / 08:00–20:00。
|
||
"""
|
||
rb = xlrd.open_workbook(src_path, formatting_info=True)
|
||
rs = rb.sheet_by_index(0)
|
||
wb = copy(rb)
|
||
ws = wb.get_sheet(0)
|
||
|
||
dept = rs.cell_value(1, 1)
|
||
emp_no = rs.cell_value(1, 2)
|
||
name = rs.cell_value(1, 3)
|
||
|
||
ordered_dates, weekdays, saturdays = build_ordered_dates(start, end)
|
||
|
||
# [STEP 1] 清除模板旧数据行
|
||
for row_idx in range(1, rs.nrows):
|
||
for col_idx in range(rs.ncols):
|
||
ws.write(row_idx, col_idx, "")
|
||
|
||
# [STEP 2] 写入新明细
|
||
for idx, day in enumerate(ordered_dates, start=1):
|
||
is_weekday = day.weekday() < 5
|
||
ot_type = "026" if is_weekday else "027"
|
||
start_time = "18:00" if is_weekday else "08:00"
|
||
end_time = "23:30" if is_weekday else "20:00"
|
||
ds = _date_str(day)
|
||
row = idx
|
||
|
||
ws.write(row, 0, str(idx))
|
||
ws.write(row, 1, dept)
|
||
ws.write(row, 2, emp_no)
|
||
ws.write(row, 3, name)
|
||
ws.write(row, 4, ot_type)
|
||
ws.write(row, 5, ds)
|
||
ws.write(row, 6, ds)
|
||
ws.write(row, 7, start_time)
|
||
ws.write(row, 8, ds)
|
||
ws.write(row, 9, end_time)
|
||
ws.write(row, 10, _overtime_hours(is_weekday))
|
||
ws.write(row, 11, "")
|
||
|
||
wb.save(out_path)
|
||
print(f"Saved: {out_path}")
|
||
print(
|
||
f"Rows: {len(ordered_dates)} "
|
||
f"(weekdays {len(weekdays)} + saturdays {len(saturdays)})"
|
||
)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
fill_overtime_workbook(
|
||
SRC,
|
||
OUT,
|
||
date(2026, 8, 21),
|
||
date(2026, 9, 20),
|
||
)
|