openpyxl-sandbox-workaround
它能做什么
这项技能是电子表格任务的备用方案,适用于代码沙盒失败的情况。它改为直接从 shell 运行 openpyxl 代码。适合在常规沙盒失败时仍需生成 Excel 文件的人。
适用场景
- ✓当代码沙盒在 openpyxl 或电子表格任务上反复失败时。
- ✓当报错涉及创建工作簿、写入单元格或保存文件时。
- ✓当我需要一项多步骤的电子表格任务,要读取已有文件并写入新数据时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/openpyxl-sandbox-workaround/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我:沙盒在生成我的电子表格时一直失败。请直接在 shell 中运行同样的代码,并保存文件。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 在 shell 的 heredoc 中写入一段内联 Python 脚本,创建或加载工作簿,添加数据,保存文件,并打印确认信息。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Openpyxl Sandbox Workaround
When to Use
Use this pattern when execute_code_sandbox repeatedly fails for openpyxl or spreadsheet manipulation tasks. The sandbox environment may have compatibility issues with certain openpyxl operations, but running Python directly via run_shell often succeeds.
Common Failure Indicators
execute_code_sandboxreturns errors related to openpyxl imports or operations- Multiple retry attempts fail with similar errors
- Errors mention workbook creation, cell writing, or file saving issues
The Workaround Pattern
Instead of using execute_code_sandbox, use run_shell with an inline Python script:
python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
# Your openpyxl code here
wb = Workbook()
ws = wb.active
ws.title = "Sheet1"
# Add data
ws['A1'] = 'Header'
ws['B1'] = 'Value'
# Save file
wb.save('output.xlsx')
print('File created successfully')
EOF
Multi-Step Spreadsheet Tasks
For complex operations involving multiple files or data processing:
python3 << 'EOF'
from openpyxl import Workbook, load_workbook
import os
# Load existing workbook if needed
if os.path.exists('input.xlsx'):
wb = load_workbook('input.xlsx')
ws = wb.active
# Process data...
# Create new workbook
wb = Workbook()
ws = wb.active
# Add data with proper formatting
for row_idx, row_data in enumerate(data, start=1):
for col_idx, value in enumerate(row_data, start=1):
ws.cell(row=row_idx, column=col_idx, value=value)
# Auto-adjust column widths
for column in ws.columns:
max_length = 0
column_letter = get_column_letter(column[0].column)
for cell in column:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = min(max_length + 2, 50)
ws.column_dimensions[column_letter].width = adjusted_width
wb.save('output.xlsx')
EOF
Best Practices
- Use heredoc syntax: The
<< 'EOF'pattern prevents variable expansion issues - Print success messages: Always include print statements to confirm completion
- Handle file paths: Use absolute paths or ensure you're in the correct working directory
- Validate output: After creation, verify the file exists and has expected content
- Error handling: Add try/except blocks for robustness in complex scripts
Verification Step
After running the script, verify the file was created:
ls -la *.xlsx
Or check file details:
python3 << 'EOF'
from openpyxl import load_workbook
wb = load_workbook('output.xlsx')
print(f"Sheets: {wb.sheetnames}")
print(f"Active sheet: {wb.active.title}")
EOF
When to Fall Back to This Pattern
- First
execute_code_sandboxattempt fails with openpyxl errors - Error persists after 1-2 retries
- Task is time-sensitive and needs a reliable solution
- The operation is straightforward file creation/modification
Limitations
- Output is limited to stdout/stderr (no direct artifact download from run_shell)
- Files are created in the current working directory
- More verbose than execute_code_sandbox for simple operations
常见问题
为什么这种方法有帮助?
指南说明沙盒可能存在兼容性问题,而直接在 shell 中运行 Python 可以避开这些问题。
可以打开已有的工作簿吗?
可以。示例会先加载输入工作簿(如果存在),再创建新内容。
怎样确认它成功了?
脚本会打印成功信息,并且可以检查保存的文件。
详情
作者的原始描述
Use run_shell with inline Python as a fallback when execute_code_sandbox fails for openpyxl/spreadsheet operations
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12902
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。