日常上课考勤中,通过每一天的座次表不仅可以提高查岗效率,考勤统计也更加的便利了。
形如下图的座次表,信息有’每一排‘’窗户‘’过道‘’门‘等干扰因素,需统计考勤表,我采用python来实现此功能。
座次表考勤统计实现步骤:
-
读取座次表:使用
read_roster
函数从Excel文件读取座次表数据,返回一个包含座次信息的DataFrame。 -
读取考勤表:使用
read_attendance
函数从Excel文件读取考勤数据,返回一个考勤字典,其中键是座次表中的姓名,值是考勤状态(如'√')。 -
更新座次表考勤数据:使用
update_roster_attendance
函数遍历每个日期的考勤数据,更新座次表中的考勤状态。 -
主函数
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)
运行结果如下