座次表考勤统计

日常上课考勤中,通过每一天的座次表不仅可以提高查岗效率,考勤统计也更加的便利了。

形如下图的座次表,信息有’每一排‘’窗户‘’过道‘’门‘等干扰因素,需统计考勤表,我采用python来实现此功能。

座次表考勤统计实现步骤:

  1. 读取座次表:使用read_roster函数从Excel文件读取座次表数据,返回一个包含座次信息的DataFrame。

  2. 读取考勤表:使用read_attendance函数从Excel文件读取考勤数据,返回一个考勤字典,其中键是座次表中的姓名,值是考勤状态(如'√')。

  3. 更新座次表考勤数据:使用update_roster_attendance函数遍历每个日期的考勤数据,更新座次表中的考勤状态。

  4. 主函数main:先读取座次表,再逐个读取考勤表并存储考勤数据,最后更新座次表中的考勤状态,并将结果保存到新的Excel文件中。

这样就完成了座次表考勤统计的实现过程。📚😊

import pandas as pd  # 先pip install pandas

# 读取座次表函数
def read_roster(roster_file):
    df_roster = pd.read_excel(roster_file)  # 读取Excel文件
    return df_roster

# 读取考勤表函数
def read_attendance(attendance_file, date):
    df_attendance = pd.read_excel(attendance_file, header=None, skiprows=3)  # 读取考勤Excel文件,跳过前3行
    attendance_dict = {}
    for row in df_attendance.itertuples(index=False, name=None):
        seat_names = row[:-1]  # 获取每一行除最后一列的数据
        for seat_name in seat_names:
            seat_name = str(seat_name).strip()  # 去除前后的空格
            if seat_name and seat_name not in ['门', '过道', '窗户', '']:  # 排除特殊字符
                attendance_dict[seat_name] = '√'  # 记录考勤
    # 请假人数少,单独人工处理
    return attendance_dict

# 更新座次表考勤数据函数
def update_roster_attendance(df_roster, attendance_dicts_by_date):
    # 遍历每个日期的考勤数据
    for date, attendance_dict in attendance_dicts_by_date.items():
        column_name = f'考勤_{date}'  # 创建考勤列名
        if column_name not in df_roster.columns:
            df_roster[column_name] = '×'  # 初始化考勤列,默认值为'×'
        for index, row in df_roster.iterrows():
            name = row['姓名']  # 获取学生姓名
            if name in attendance_dict:
                df_roster.at[index, column_name] = attendance_dict[name]  # 更新考勤状态

    return df_roster

# 主函数
def main(roster_file, attendance_files_with_dates):
    df_roster = read_roster(roster_file)  # 读取座次表
    attendance_dicts_by_date = {}
    # 读取各个日期的考勤数据
    for file, date in attendance_files_with_dates:
        attendance_dict = read_attendance(file, date)
        attendance_dicts_by_date[date] = attendance_dict
    # 更新座次表的考勤状态
    df_roster_updated = update_roster_attendance(df_roster, attendance_dicts_by_date)

    output_file = roster_file.replace('.xlsx', '_updated.xlsx')  # 生成输出文件名
    df_roster_updated.to_excel(output_file, index=False)  # 保存更新后的座次表
    print(f"Updated roster saved to {output_file}")

# 输入文件路径和考勤文件与日期列表
roster_file = 'D:\\桌面\\大数据处理技术-学生序号.xlsx'
attendance_files_with_dates = [
    ('D:\\桌面\\考勤表\\***考勤表10.8.xlsx', '2024-10-08'),
    ('D:\\桌面\\考勤表\\***考勤表10.10.xlsx', '2024-10-10'),  # 每一行包括地址和时间
    ('D:\\桌面\\考勤表\\***考勤表11.26.xlsx', '2024-11-26'),
    ('D:\\桌面\\考勤表\\***考勤表12.5.xlsx', '2024-12-5'),
    ('D:\\桌面\\考勤表\\***考勤表12.10.xlsx', '2024-12-10'),
]

# 运行主函数
main(roster_file, attendance_files_with_dates)

运行结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值