
第12讲:Excel 自动化 Skill 开发
掌握 Excel 场景的 Skill 开发,实现数据清洗、报表生成、格式转换等自动化操作,让繁琐的表格处理工作变得轻松高效。
一、场景分析
1.1 用户痛点
在日常办公中,Excel 处理是最耗时的工作之一:
- 数据清洗痛苦:从各部门收集的数据格式不统一,需要手动整理、去重、填充空值
- 报表制作繁琐:每周/每月需要重复制作相同格式的报表,复制粘贴容易出错
- 格式转换困难:需要将 Excel 转换为 PDF、CSV 等其他格式,操作步骤多
- 数据合并复杂:多个表格需要按条件合并,VLOOKUP 公式写起来头大
- 批量处理低效:几百个文件需要统一修改格式,手动操作不现实
1.2 典型应用场景
| 场景 | 需求描述 | Skill 价值 |
|---|
| 销售报表 | 汇总各区域销售数据,生成可视化图表 | 一键生成,自动更新 |
| 财务报表 | 合并多个科目表,计算汇总数据 | 减少人工计算错误 |
| 人事统计 | 统计考勤、绩效、薪资数据 | 自动化数据处理 |
| 库存管理 | 跟踪库存变动,预警低库存商品 | 实时监控,自动提醒 |
| 数据迁移 | 将旧系统数据转换为新系统格式 | 批量转换,格式校验 |
二、核心功能设计
2.1 Skill 功能清单
一个完整的 Excel 自动化 Skill 应该具备以下功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 📊 Excel 智能助手 ├── 数据清洗 │ ├── 去除重复行 │ ├── 填充空值 │ ├── 格式统一化 │ └── 异常数据标记 ├── 报表生成 │ ├── 数据透视表 │ ├── 图表生成 │ ├── 条件格式 │ └── 自动汇总 ├── 格式转换 │ ├── Excel ↔ CSV │ ├── Excel ↔ PDF │ ├── Excel ↔ JSON │ └── 批量转换 ├── 数据合并 │ ├── 多表合并 │ ├── 按条件汇总 │ ├── 数据对比 │ └── 差异报告 └── 批量处理 ├── 批量重命名 ├── 批量格式调整 ├── 批量公式填充 └── 批量保护设置
|
2.2 用户交互流程
1 2 3 4
| 用户输入 → 意图识别 → 参数提取 → 执行操作 → 结果返回 ↓ ↓ ↓ ↓ ↓ "帮我合并 识别为 提取文件 调用合并 返回合并 这三个表" 合并操作 路径和规则 功能模块 后的文件
|
三、技术实现
3.1 技术选型
Excel 处理的核心技术栈:
| 平台 | 技术方案 | 特点 |
|---|
| Coze | Python + openpyxl/xlrd | 功能全面,支持复杂操作 |
| OpenClaw | Python + pandas | 数据处理能力强 |
| 飞书 CLI | 飞书表格 API | 与飞书生态深度集成 |
3.2 Coze 平台实现
3.2.1 插件配置
在 Coze 中创建 Excel 处理 Skill,需要配置以下插件:
- 代码执行节点:使用 Python 处理 Excel 文件
- 文件处理节点:读取和保存 Excel 文件
- 大模型节点:理解用户意图,提取处理参数
3.2.2 核心代码示例
数据清洗功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import pandas as pd import openpyxl from openpyxl.styles import PatternFill, Font
def clean_excel(file_path, operations): """ Excel 数据清洗主函数 Args: file_path: Excel 文件路径 operations: 清洗操作列表 Returns: 清洗后的文件路径 """ df = pd.read_excel(file_path) for op in operations: if op['type'] == 'remove_duplicates': subset = op.get('columns', None) df = df.drop_duplicates(subset=subset) elif op['type'] == 'fill_na': strategy = op.get('strategy', 'forward') if strategy == 'forward': df = df.fillna(method='ffill') elif strategy == 'backward': df = df.fillna(method='bfill') elif strategy == 'value': fill_value = op.get('value', 0) df = df.fillna(fill_value) elif op['type'] == 'standardize': column = op['column'] format_type = op['format'] if format_type == 'date': df[column] = pd.to_datetime(df[column]) elif format_type == 'number': df[column] = pd.to_numeric(df[column], errors='coerce') elif format_type == 'text': df[column] = df[column].astype(str).str.strip() elif op['type'] == 'mark_anomaly': column = op['column'] condition = op['condition'] df[f'{column}_is_anomaly'] = df.eval(condition) output_path = file_path.replace('.xlsx', '_cleaned.xlsx') df.to_excel(output_path, index=False) return output_path
|
报表生成功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| def generate_report(data_file, report_config): """ 自动生成数据报表 Args: data_file: 数据源文件 report_config: 报表配置 """ df = pd.read_excel(data_file) pivot = pd.pivot_table( df, values=report_config['values'], index=report_config['index'], columns=report_config.get('columns'), aggfunc=report_config.get('aggfunc', 'sum') ) import matplotlib.pyplot as plt plt.figure(figsize=(10, 6)) pivot.plot(kind='bar') plt.title(report_config['title']) plt.tight_layout() chart_path = data_file.replace('.xlsx', '_chart.png') plt.savefig(chart_path) report_path = data_file.replace('.xlsx', '_report.xlsx') with pd.ExcelWriter(report_path, engine='openpyxl') as writer: df.to_excel(writer, sheet_name='原始数据', index=False) pivot.to_excel(writer, sheet_name='透视表') return { 'report_file': report_path, 'chart_file': chart_path }
|
多表合并功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| def merge_excel_files(file_list, merge_config): """ 合并多个 Excel 文件 Args: file_list: 文件路径列表 merge_config: 合并配置 """ dataframes = [] for file_path in file_list: df = pd.read_excel(file_path) df['source_file'] = file_path dataframes.append(df) merge_type = merge_config.get('type', 'concat') if merge_type == 'concat': result = pd.concat(dataframes, ignore_index=True) elif merge_type == 'merge': result = dataframes[0] for df in dataframes[1:]: result = pd.merge( result, df, on=merge_config['on'], how=merge_config.get('how', 'inner') ) if merge_config.get('remove_duplicates', False): result = result.drop_duplicates() output_path = merge_config['output_path'] result.to_excel(output_path, index=False) return output_path
|
3.3 OpenClaw 平台实现
OpenClaw 的 Excel Skill 开发更加简洁:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from openclaw import Skill, Tool import pandas as pd
class ExcelSkill(Skill): name = "Excel智能助手" description = "自动化处理 Excel 文件" @Tool def clean_data(self, file: str, operations: list) -> str: """清洗 Excel 数据""" df = pd.read_excel(file) return "清洗完成" @Tool def merge_files(self, files: list, output: str) -> str: """合并多个 Excel 文件""" dfs = [pd.read_excel(f) for f in files] result = pd.concat(dfs) result.to_excel(output, index=False) return f"已合并到 {output}"
|
3.4 飞书 CLI 实现
飞书 CLI 可以直接操作飞书表格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from lark_cli import Skill, SpreadsheetAPI
class FeishuExcelSkill(Skill): def __init__(self): self.api = SpreadsheetAPI() def sync_data(self, spreadsheet_token: str, data: list): """同步数据到飞书表格""" self.api.write_range( spreadsheet_token=spreadsheet_token, range="Sheet1!A1", values=data ) return "同步成功" def generate_chart(self, spreadsheet_token: str, config: dict): """在飞书表格中生成图表""" chart = self.api.create_chart( spreadsheet_token=spreadsheet_token, chart_type=config['type'], data_range=config['range'] ) return chart
|
四、Prompt 设计
4.1 系统 Prompt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 你是 Excel 智能助手,专门帮助用户自动化处理 Excel 文件。
你可以执行以下操作: 1. 数据清洗:去重、填充空值、格式统一、异常标记 2. 报表生成:透视表、图表、汇总统计 3. 格式转换:Excel ↔ CSV/PDF/JSON 4. 数据合并:多表合并、条件汇总 5. 批量处理:批量重命名、格式调整
处理流程: 1. 理解用户的 Excel 处理需求 2. 询问必要的参数(文件路径、处理规则等) 3. 确认操作内容 4. 执行处理 5. 返回处理结果和文件
注意事项: - 处理前备份原始文件 - 大数据量时提示用户可能需要等待 - 提供处理摘要(处理了多少行、哪些变更)
|
4.2 意图识别示例
| 用户输入 | 识别意图 | 提取参数 |
|---|
| "帮我把这个表里的重复数据删掉" | 数据清洗-去重 | 文件路径、去重列 |
| "把这三个销售表合并成一个" | 数据合并 | 文件列表、合并方式 |
| "生成一个按月份汇总的报表" | 报表生成 | 数据源、汇总维度 |
| "把 Excel 转成 PDF" | 格式转换 | 源文件、目标格式 |
五、实战案例
5.1 案例一:销售数据清洗
场景:销售部门每月收到各区域提交的 Excel 报表,需要合并并清洗。
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| def process_sales_data(file_list): merged = merge_excel_files(file_list, { 'type': 'concat', 'remove_duplicates': True }) cleaned = clean_excel(merged, [ {'type': 'remove_duplicates', 'columns': ['订单号']}, {'type': 'fill_na', 'strategy': 'value', 'value': 0}, {'type': 'standardize', 'column': '日期', 'format': 'date'}, {'type': 'mark_anomaly', 'column': '金额', 'condition': '金额 < 0'} ]) report = generate_report(cleaned, { 'values': ['金额', '数量'], 'index': ['区域', '月份'], 'aggfunc': 'sum', 'title': '销售数据汇总' }) return report
|
5.2 案例二:财务报表自动化
场景:财务每月需要从多个系统导出数据,生成管理报表。
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| def generate_finance_report(config): """自动生成财务报表""" gl_data = pd.read_excel(config['general_ledger']) ar_data = pd.read_excel(config['ar_file']) ap_data = pd.read_excel(config['ap_file']) validate_data(gl_data, ar_data, ap_data) reports = { '资产负债表': generate_balance_sheet(gl_data), '利润表': generate_income_statement(gl_data), '现金流量表': generate_cash_flow(gl_data), '应收账款分析': generate_ar_analysis(ar_data), '应付账款分析': generate_ap_analysis(ap_data) } pdf_path = export_to_pdf(reports, config['output_pdf']) return pdf_path
|
六、多平台适配对比
| 功能 | Coze | OpenClaw | 飞书 CLI |
|---|
| 本地文件处理 | ✅ | ✅ | ❌ |
| 飞书表格操作 | ❌ | ❌ | ✅ |
| 复杂数据处理 | ✅ | ✅ | 中等 |
| 图表生成 | ✅ | ✅ | ✅ |
| PDF 转换 | ✅ | ✅ | 需插件 |
| 批量处理 | ✅ | ✅ | 中等 |
七、实战练习
练习 1:基础数据清洗
创建一个 Skill,实现以下功能:
- 读取用户上传的 Excel 文件
- 自动检测并去除重复行
- 填充所有空值为 0
- 将日期列统一为 YYYY-MM-DD 格式
- 返回清洗后的文件
练习 2:销售报表生成
创建一个 Skill,实现以下功能:
- 读取销售数据
- 按产品类别和月份生成透视表
- 生成柱状图展示销售额
- 标记销售额超过 10 万的记录
- 导出为 PDF 报表
练习 3:多表合并工具
创建一个 Skill,实现以下功能:
- 接收多个 Excel 文件
- 按指定列(如"客户ID")合并
- 处理列名冲突(添加前缀区分来源)
- 生成合并报告(显示合并前后的行数)
- 返回合并后的文件
八、常见问题
Q1:处理大文件时内存不足怎么办?
解决方案:
- 使用
pandas.read_excel() 的 chunksize 参数分块读取 - 只读取需要的列:
usecols=['列A', '列B'] - 考虑使用 Dask 进行分布式处理
Q2:如何保留 Excel 的格式和公式?
解决方案:
- 使用
openpyxl 而不是 pandas 处理格式 - 读取时保留公式:
data_only=False - 复制单元格样式到新的工作簿
Q3:中文乱码怎么处理?
解决方案:
- 明确指定编码:
encoding='utf-8' 或 'gbk' - CSV 文件添加 BOM:
encoding='utf-8-sig' - 检查源文件的编码格式
九、下节预告
下一讲我们将学习 PDF 智能处理 Skill 开发,包括:
- PDF 内容提取与解析
- PDF 合并、拆分、旋转
- PDF 与 Word/Excel 互转
- OCR 文字识别集成
加入学习群
👉 加入AI编程学习交流群

本讲是《AI Skills 从入门到实践》系列课程的第12讲。
🎓 AI 编程实战课程
想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!