影刀RPA 流程文档自动生成:让流程自我记录

📅 2026/7/21 22:14:21
影刀RPA 流程文档自动生成:让流程自我记录
影刀RPA 流程文档自动生成让流程自我记录作者林焱一、什么情况需要自动生成流程文档大多数RPA团队的痛点不是做不出自动化而是做出来后没有文档。三个月后流程出问题写流程的人已经离职谁也不知道这个流程在干什么、为什么这么设计。让影刀在运行过程中自动记录关键步骤信息可以生成自描述的流程文档省去手工写文档的麻烦。拼多多店群自动化上架方案自动文档的价值流程说明书适合非技术人员看的业务描述运行日志方便故障排查操作截图记录留存操作证据变更记录记录每次修改了什么二、实战一运行时自动生成操作日志设计思路在每个关键步骤前后插入日志记录代码运行完成后汇总生成Markdown文档。importdatetimeimportosclassFlowLogger:def__init__(self,flow_name):self.flow_nameflow_name self.start_timedatetime.datetime.now()self.steps[]self.screenshots[]self.errors[]deflog_step(self,step_no,step_name,status成功,detail,screenshot_pathNone):记录一个步骤self.steps.append({step_no:step_no,step_name:step_name,status:status,detail:detail,time:datetime.datetime.now().strftime(%H:%M:%S),screenshot:screenshot_path})deflog_error(self,step_name,error_msg,screenshot_pathNone):记录错误self.errors.append({step:step_name,error:str(error_msg),time:datetime.datetime.now().strftime(%H:%M:%S),screenshot:screenshot_path})self.log_step(len(self.steps)1,step_name,status❌失败,detailstr(error_msg)[:200],screenshot_pathscreenshot_path)defgenerate_report(self,output_dirC:\\流程报告):生成Markdown格式的运行报告os.makedirs(output_dir,exist_okTrue)end_timedatetime.datetime.now()duration(end_time-self.start_time).seconds success_countsum(1forsinself.stepsif成功ins[status])fail_countlen(self.errors)# 生成Markdown内容md_contentf# 流程运行报告{self.flow_name}**运行时间**{self.start_time.strftime(%Y-%m-%d %H:%M:%S)}**耗时**{duration}秒 **执行步骤数**{len(self.steps)}**成功**{success_count}步 | **失败**{fail_count}步 **整体状态**{✅ 成功iffail_count0else❌ 存在失败步骤}--- ## 执行步骤详情 | 步骤 | 名称 | 状态 | 时间 | 备注 | |------|------|------|------|------| forstepinself.steps:screenshot_linkf[截图]({step[screenshot]})ifstep[screenshot]else-md_contentf|{step[step_no]}|{step[step_name]}|{step[status]}|{step[time]}|{step[detail][:50] or screenshot_link}|\nifself.errors:md_content\n## 错误详情\n\nforerrinself.errors:md_contentf**{err[time]}** - **{err[step]}**\n{err[error]}\n\n# 保存报告filenamef{self.flow_name}_{self.start_time.strftime(%Y%m%d_%H%M%S)}.mdreport_pathos.path.join(output_dir,filename)withopen(report_path,w,encodingutf-8)asf:f.write(md_content)print(f报告已生成{report_path})returnreport_path# 在影刀流程中使用loggerFlowLogger(月末发票批量下载)try:# Step 1: 登录系统yingdao.open_url(https://invoice.company.com)logger.log_step(1,登录发票系统)# Step 2: 筛选本月发票yingdao.find_element(#month-filter).send_keys(2024-12)yingdao.find_element(#search-btn).click()logger.log_step(2,筛选本月发票)# Step 3: 下载yingdao.find_element(#download-all).click()screenshotyingdao.screenshot(C:\\报告截图\\download_confirm.png)logger.log_step(3,批量下载发票,detailf已触发下载,screenshot_pathscreenshot)exceptExceptionase:screenshotyingdao.screenshot(C:\\报告截图\\error.png)logger.log_error(执行过程,e,screenshot_pathscreenshot)finally:reportlogger.generate_report()yingdao.open_file(report)# 自动打开报告查看三、实战二生成流程说明书给业务人员看的流程说明书用自然语言描述每步操作。defgenerate_process_guide(flow_name,steps_config,output_path): steps_config: [{name: 步骤名, action: 操作描述, note: 注意事项}] fromdocximportDocumentfromdocx.sharedimportPt docDocument()doc.add_heading(f{flow_name}操作说明书,0)doc.add_paragraph(f最后更新{datetime.date.today()})doc.add_paragraph(本文档由影刀RPA自动生成如有更新请重新运行生成流程。)doc.add_heading(流程步骤,level1)fori,stepinenumerate(steps_config,1):![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/bd2bdc6d97c84cad99fb63af4973ef54.png#pic_center)doc.add_heading(f第{i}步{step[name]},level2)doc.add_paragraph(step[action])ifstep.get(note):pdoc.add_paragraph()p.add_run(⚠️ 注意).boldTruep.add_run(step[note])ifstep.get(screenshot):doc.add_picture(step[screenshot],widthInches(5))doc.save(output_path)print(f说明书已生成{output_path})TEMU店群如何管理运营四、踩坑记录坑1截图文件越积越多每次运行都截图一个月下来几千张图片占据大量空间。策略只在失败时截图或每隔5步截一次综述截图成功的全量步骤不截图。坑2Markdown报告在Windows记事本乱码保存时指定UTF-8编码提醒用VSCode或浏览器打开Markdown文件。坑3日志里不要记录密码logger.log_step()的detail字段要过滤掉密码、API Key等敏感信息用***替代。坑4报告过长没人看关键是摘要运行时长、成功率、错误摘要放在最前面。详细步骤折叠在后面方便快速判断是否正常。适用人群RPA开发者、流程设计师、运维团队难度⭐⭐代码逻辑简单养成习惯最难