reliable-excel-workbook-creation
它能做什么
这项技能展示 AI 智能体如何用一个简洁的 Python 脚本制作复杂的 Excel 文件。它适合需要包含数据和格式的多工作表工作簿的人。
适用场景
- ✓当你需要一个包含多个工作表、数据和格式的 Excel 工作簿的时候。
- ✓当文件读取器无法正确打开现有的 XLSX 文件的时候。
- ✓当多步骤的工作簿任务在沙箱中失败的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reliable-excel-workbook-creation/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的预算制作一个包含三个工作表的 Excel 工作簿,表头加粗。请用脚本把它保存到我的工作区。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会切换到工作区文件夹,并在 shell 中运行一个 Python 脚本,该脚本会创建工作簿、添加数据、设置格式,并使用完整的文件路径保存。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Reliable Excel Workbook Creation via Shell
When to Use This Pattern
Use this technique when you need to:
- Create complex Excel workbooks with multiple sheets
- Perform multi-step workbook operations (create, populate, format, save)
- Work with XLSX files that
read_filecannot handle properly - Avoid failures from
execute_code_sandboxon persistent file operations
Avoid: read_file for XLSX files, execute_code_sandbox for multi-step workbook creation
Core Pattern
cd /workspace && python3 << 'EOF'
from openpyxl import Workbook
# Your Excel operations here
wb = Workbook()
# ... create sheets, add data, save
wb.save('/workspace/filename.xlsx')
EOF
Step-by-Step Instructions
Step 1: Change to Workspace Directory
Always start with cd /workspace && to ensure file paths resolve correctly.
Step 2: Use Heredoc with Single-Quoted EOF
Use << 'EOF' (single quotes) to prevent shell variable expansion within the Python code.
Step 3: Write Complete Python Script
Include all imports, workbook creation, data population, and save operations in one script.
Step 4: Use Absolute Paths for Save Operations
Always save with full path: /workspace/your_filename.xlsx
Complete Example
cd /workspace && python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill
# Create workbook
wb = Workbook()
# Create multiple sheets
sheets = ['Summary', 'Data', 'Analysis', 'Settings']
for sheet_name in sheets:
wb.create_sheet(title=sheet_name)
# Remove default sheet if needed
if 'Sheet' in wb.sheetnames:
del wb['Sheet']
# Populate data
ws = wb['Data']
ws['A1'] = 'Item'
ws['B1'] = 'Value'
ws['A2'] = 'Revenue'
ws['B2'] = 100000
# Apply formatting
header_font = Font(bold=True)
for cell in ws['1:1']:
cell.font = header_font
# Save workbook
wb.save('/workspace/report.xlsx')
print('Workbook created successfully: /workspace/report.xlsx')
EOF
Why This Works
| Approach | Problem | Solution |
|---|---|---|
read_file |
Cannot parse XLSX binary format | Don't use for Excel files |
execute_code_sandbox |
File persistence issues, multi-step failures | Use run_shell instead |
run_shell with heredoc |
✓ Reliable, full control, persistent files | Use this pattern |
Common Operations
Create Multi-Sheet Workbook
cd /workspace && python3 << 'EOF'
from openpyxl import Workbook
wb = Workbook()
sheet_names = ['Sheet1', 'Sheet2', 'Sheet3']
for name in sheet_names:
wb.create_sheet(title=name)
wb.save('/workspace/multi_sheet.xlsx')
EOF
Read and Modify Existing Workbook
cd /workspace && python3 << 'EOF'
from openpyxl import load_workbook
wb = load_workbook('/workspace/existing.xlsx')
ws = wb.active
ws['A1'] = 'Updated Value'
wb.save('/workspace/existing.xlsx')
EOF
Add Formulas
cd /workspace && python3 << 'EOF'
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = 100
ws['A2'] = 200
ws['A3'] = '=SUM(A1:A2)'
wb.save('/workspace/with_formulas.xlsx')
EOF
Troubleshooting
- File not found: Ensure you use
/workspace/prefix in all paths - Permission errors: Verify workspace directory is writable
- Module not found: openpyxl is pre-installed in the environment
- Heredoc issues: Use single-quoted
'EOF'to prevent shell interpretation
Best Practices
- Always use absolute paths (
/workspace/filename.xlsx) - Single-quoted heredoc (
<< 'EOF') prevents variable expansion - Complete script in one heredoc - avoid splitting operations
- Print confirmation after save to verify success
- Handle exceptions for production scripts
常见问题
为什么要使用完整的保存路径?
完整路径可以确保文件保存在你预期的位置,而不是其他文件夹。
脚本可以设置单元格格式吗?
可以。这项技能的示例会在工作簿中设置字体、填充和对齐方式。
为什么这里不用文件读取器处理 XLSX?
这项技能指出,读取器有时无法正确处理 XLSX 文件,因此改为直接写入文件。
详情
作者的原始描述
Use run_shell with cd and heredoc syntax for complex Excel operations when read_file fails on XLSX or execute_code_sandbox fails on multi-step workbook creation
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12966
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。