影刀RPA Excel批注管理:添加与读取批注
影刀RPA Excel批注管理添加与读取批注作者林焱什么情况用什么审核报表时需要给异常数据加备注说明但又不希望备注显示在单元格里影响数据格式——批注Comment就是干这个的。在影刀RPA自动化流程中经常需要批量添加批注标记数据来源、审核意见或者读取已有批注提取审核信息。适用场景数据审核自动化、批量标注异常原因、读取批注提取审核意见、报表交接时自动添加说明。怎么做添加批注店群矩阵自动化突破运营极限importopenpyxlfromopenpyxl.commentsimportComment wbopenpyxl.load_workbook(rC:\Reports\audit.xlsx)wswb.active# 给单个单元格添加批注ws[D3].commentComment(数据来源7月销售系统导出\n核对人张三,审核系统)# 批量添加批注标记异常数据forrowinrange(2,ws.max_row1):amountws.cell(rowrow,column4).value# 金额列ifamountisnotNoneandamount100000:cellws.cell(rowrow,column4)cell.commentComment(f大额交易提醒金额{amount}元超过阈值10万\n请核实交易真实性,风控系统)wb.save(rC:\Reports\audit.xlsx)读取批注# 读取所有批注wbopenpyxl.load_workbook(rC:\Reports\audit.xlsx)wswb.active comments_data[]forrowinws.iter_rows():forcellinrow:ifcell.comment:comments_data.append({cell:cell.coordinate,author:cell.comment.author,text:cell.comment.text,row:cell.row,col:cell.column})# 打印所有批注forcincomments_data:print(f单元格{c[cell]}(作者:{c[author]}):{c[text]})批注带格式fromopenpyxl.commentsimportCommentfromopenpyxl.comments.comment_textimportCommentText# 带换行和格式的批注rich_textCommentText()rich_text.add_text(审核意见\n,boldTrue,size11)rich_text.add_text(1. 数据异常请核实\n,size10,colorFF0000)rich_text.add_text(2. 已通知业务部门确认,size10,color0000FF)commentComment(rich_text,审核员)comment.width300# 批注框宽度comment.height100# 批注框高度ws[D5].commentcomment提取批注生成审核报告defextract_comments_to_report(excel_path,output_path):提取所有Sheet的批注生成审核报告wbopenpyxl.load_workbook(excel_path)report_lines[审核意见汇总报告,*50,]forwsinwb.worksheets:sheet_comments[]forrowinws.iter_rows():forcellinrow:ifcell.comment:# 获取该行关键信息row_data[]forcinws[row[0].row]:ifc.valueisnotNone:row_data.append(str(c.value))sheet_comments.append({cell:cell.coordinate,text:cell.comment.text,author:cell.comment.author,context: | .join(row_data[:5])# 前5列作为上下文})ifsheet_comments:report_lines.append(f\n【工作表{ws.title}】)report_lines.append(f共{len(sheet_comments)}条批注\n)fori,cinenumerate(sheet_comments,1):report_lines.append(f{i}. 单元格{c[cell]})report_lines.append(f 作者{c[author]})report_lines.append(f 批注{c[text]})report_lines.append(f 行数据{c[context]})report_lines.append()iflen(report_lines)3:report_lines.append(未发现批注。)withopen(output_path,w,encodingutf-8)asf:f.write(\n.join(report_lines))returnoutput_path# 在影刀中调用extract_comments_to_report(rC:\Reports\audit.xlsx,rC:\Reports\audit_comments.txt)批量清除批注defclear_all_comments(file_path):清除所有批注wbopenpyxl.load_workbook(file_path)count0forwsinwb.worksheets:forrowinws.iter_rows():forcellinrow:ifcell.comment:cell.commentNonecount1wb.save(file_path)returnf已清除{count}条批注有什么坑坑1批注作者不能为空# 错误不传作者名ws[A1].commentComment(这是批注内容)# 报错author必填# 正确必须指定作者ws[A1].commentComment(这是批注内容,系统)坑2批注文本包含换行符显示异常temu店群自动化报活动案例在批注里用\n换行但Excel打开后换行没生效——因为批注的换行需要特殊处理# 确保批注框设置为自动换行commentComment(第一行\n第二行\n第三行,系统)# openpyxl 3.x 默认支持\n换行但如果不行# 替代方案用富文本fromopenpyxl.comments.comment_textimportCommentText ctCommentText()ct.add_text(第一行\n)ct.add_text(第二行\n)ct.add_text(第三行)ws[A1].commentComment(ct,系统)坑3读取批注时文件被锁定如果Excel文件正被打开openpyxl读取批注时会报PermissionError。在影刀中需要先确保文件未被占用importosdefsafe_load_workbook(file_path):安全加载工作簿try:returnopenpyxl.load_workbook(file_path)exceptPermissionError:# 文件被占用尝试复制后读取importshutil temp_pathfile_path.tmpshutil.copy2(file_path,temp_path)wbopenpyxl.load_workbook(temp_path)os.remove(temp_path)returnwb坑4批注数量过多导致文件打开缓慢一个工作表添加了上千条批注Excel打开时卡顿严重。批注本质是XML节点数量过多会拖慢文件。建议# 数据量大时改用单独的备注列代替批注# 而不是给每个异常单元格都加批注# 方案在第N列写备注文字forrowinrange(2,ws.max_row1):amountws.cell(rowrow,column4).valueifamountisnotNoneandamount100000:ws.cell(rowrow,column8).valuef大额交易{amount}元# 备注列ws.cell(rowrow,column8).fontFont(colorFF0000)坑5批注框大小不自适应默认批注框很小长文本被截断看不到全部内容。需要手动设置大小commentComment(这是一段很长的批注内容需要设置足够的宽度和高度才能完整显示...,系统)comment.width400# 像素comment.height150ws[A1].commentcomment