影刀RPA 企微考勤自动化:打卡数据提取与异常统计

📅 2026/7/12 5:21:13
影刀RPA 企微考勤自动化:打卡数据提取与异常统计
title: “影刀RPA 企微考勤自动化打卡数据提取与异常统计”date: 2026-07-01author: 林焱影刀RPA 企微考勤自动化打卡数据提取与异常统计HR每月月底要统计全员考勤从企业微信后台导出数据、筛选迟到早退、算缺卡次数……手工操作一两小时。影刀对接企微API可以自动完成这套流程HR只需要检查结果。什么情况用什么适合自动化的考勤场景月度/周度考勤汇总报表迟到早退自动预警通知打卡异常漏打卡自动提醒员工补卡多部门考勤数据合并统计不适合的场景拼多多店群自动化上架方案需要人工判断请假是否合理涉及薪资敏感计算需要HR确认怎么做第一步申请企微API权限进入企业微信管理后台「应用管理」→ 创建或选择自建应用申请权限打卡→ 获取打卡记录通讯录→ 获取员工列表记录下corpid和corpsecret方法1拉取打卡记录【影刀操作】新建流程「每月考勤统计」添加【Python】指令获取access_tokenimportrequests corpidyour_corpidcorpsecretyour_corpsecretresprequests.get(fhttps://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid{corpid}corpsecret{corpsecret})resultresp.json()access_tokenresult[access_token]yda.set_variable(wx_token,access_token)print(fToken获取成功有效期7200秒)添加【Python】指令拉取考勤数据importrequestsimportjsonfromdatetimeimportdatetime,timedelta tokenyda.get_variable(wx_token)# 计算上个月的时间范围todaydatetime.now()first_day_this_monthtoday.replace(day1)last_month_endfirst_day_this_month-timedelta(days1)last_month_startlast_month_end.replace(day1)start_tsint(last_month_start.timestamp())end_tsint(last_month_end.replace(hour23,minute59,second59).timestamp())# 先获取员工列表dept_resprequests.get(fhttps://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token{token}department_id1fetch_child1)user_ids[u[userid]foruindept_resp.json().get(userlist,[])]# 分批拉取打卡记录每批最多100人all_records[]batch_size50foriinrange(0,len(user_ids),batch_size):batchuser_ids[i:ibatch_size]payload{useridlist:batch,starttime:start_ts,endtime:end_ts}resprequests.post(fhttps://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token{token},jsonpayload)recordsresp.json().get(checkindata,[])all_records.extend(records)print(f已拉取{len(all_records)}条打卡记录)yda.set_variable(checkin_records,json.dumps(all_records,ensure_asciiFalse))方法2统计异常打卡【影刀操作】添加【Python】指令分析考勤数据importjsonimportpandasaspdfromdatetimeimportdatetime recordsjson.loads(yda.get_variable(checkin_records))# 转换为DataFramedfpd.DataFrame(records)# checkin_time是时间戳转换为可读时间df[checkin_time_dt]pd.to_datetime(df[checkin_time],units,utcTrue).dt.tz_convert(Asia/Shanghai)df[date]df[checkin_time_dt].dt.date df[time]df[checkin_time_dt].dt.time df[hour]df[checkin_time_dt].dt.hour# 识别上班打卡6-12点和下班打卡15-23点morning_checkindf[df[hour].between(6,11)].copy()evening_checkindf[df[hour].between(15,23)].copy()# 按人按天汇总morning_by_daymorning_checkin.groupby([userid,date])[time].min().reset_index()morning_by_day.columns[userid,date,checkin_time]evening_by_dayevening_checkin.groupby([userid,date])[time].max().reset_index()evening_by_day.columns[userid,date,checkout_time]# 标记迟到上班打卡晚于9:10importdatetimeasdt late_thresholddt.time(9,10)morning_by_day[is_late]morning_by_day[checkin_time]late_threshold# 标记早退下班打卡早于18:00early_thresholddt.time(18,0)evening_by_day[is_early]evening_by_day[checkout_time]early_threshold# 汇总统计late_summarymorning_by_day.groupby(userid)[is_late].sum().reset_index()late_summary.columns[userid,late_count]early_summaryevening_by_day.groupby(userid)[is_early].sum().reset_index()early_summary.columns[userid,early_count]summarylate_summary.merge(early_summary,onuserid,howouter).fillna(0)# 保存结果summary.to_excel(rC:\考勤报告\异常考勤统计.xlsx,indexFalse)print(f统计完成共{len(summary)}人迟到合计{summary[late_count].sum()}次)yda.set_variable(abnormal_users,summary[summary[late_count]0][userid].tolist())方法3自动发消息提醒员工补卡【影刀操作】添加【循环】指令遍历异常员工在循环内添加【Python】指令importrequests tokenyda.get_variable(wx_token)user_idcurrent_user# 发送企微消息payload{touser:user_id,msgtype:text,agentid:your_agent_id,text:{content:f【考勤提醒】您本月有打卡异常记录请及时在企业微信「打卡」功能中申请补卡或说明情况。如有疑问请联系HR。}}resprequests.post(fhttps://qyapi.weixin.qq.com/cgi-bin/message/send?access_token{token},jsonpayload)print(f已通知{user_id}响应{resp.json()[errcode]})有什么坑坑1打卡类型混淆TEMU店群如何管理运营企微打卡有「固定时间制」「按班次」「自由签到」等多种类型不同类型的打卡数据结构不同要分别处理。坑2跨天班次处理做夜班的员工下班打卡可能是第二天凌晨直接按日期分组会把数据割裂。解决方法对于20:00以后的打卡归属日期往前算一天。坑3接口每次最多拉31天企微打卡接口单次请求时间范围不能超过31天月度统计要分两次请求再合并。坑4员工太多接口超时几千人的公司分批请求加统计分析耗时较长Token可能过期。解决方法每处理200人刷新一次Token。总结企微考勤自动化的核心是API拉取打卡原始数据 → Pandas清洗分析 → 生成报表 → 发消息通知。关键在于理解企微的打卡数据结构班次规则、时间戳格式、分批限制搞清楚这些剩下的就是数据处理了。