excel-heredoc-workaround
它能做什么
这项技能是在代码沙箱出现问题时,使用 openpyxl 制作 Excel 工作簿的变通方法。它适合需要带结构化表头和样式的电子表格的人。
适用场景
- ✓当因 openpyxl 问题导致沙箱中创建 .xlsx 文件失败的时候。
- ✓当我需要带样式表头、边框和日程布局的电子表格的时候。
- ✓当我可以直接在这台电脑的 shell 中运行 Python 的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/excel-heredoc-workaround/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请制作一份带样式表头和边框的 Excel 日程表。沙箱失败了,请从 shell 运行。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 首先尝试沙箱,如果失败,就在 shell 中以内联方式运行 openpyxl 脚本,创建工作簿、设置表头样式并保存文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Excel Heredoc Workaround
Purpose
When creating Excel files with openpyxl, execute_code_sandbox may fail due to environment issues. This skill provides a robust workaround: fall back to run_shell with inline Python heredoc scripts to create properly formatted spreadsheets with styling.
When to Use
- You need to create
.xlsxfiles with openpyxl execute_code_sandboxfails with openpyxl-related errors- You need styling, formatting, or complex spreadsheet features
- Direct shell execution with Python heredoc is available
Step-by-Step Instructions
Step 1: Attempt execute_code_sandbox First
Try creating the Excel file using execute_code_sandbox:
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill
wb = Workbook()
ws = wb.active
ws.title = "Data"
# Add data and styling
ws['A1'] = "Header"
ws['A1'].font = Font(bold=True)
wb.save("output.xlsx")
print("ARTIFACT_PATH:output.xlsx")
Step 2: If Sandbox Fails, Use run_shell with Heredoc
When execute_code_sandbox fails, switch to run_shell with a Python heredoc:
python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
# Create workbook
wb = Workbook()
ws = wb.active
ws.title = "Schedule"
# Add headers with styling
headers = ["Task", "Date", "Status", "Priority"]
for col, header in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=header)
cell.font = Font(bold=True, size=12)
cell.fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
cell.font = Font(bold=True, color="FFFFFF")
cell.alignment = Alignment(horizontal="center")
# Add data rows
data = [
["Cleanup Zone A", "2024-01-15", "Pending", "High"],
["Cleanup Zone B", "2024-01-16", "Complete", "Medium"],
]
for row_idx, row_data in enumerate(data, 2):
for col_idx, value in enumerate(row_data, 1):
cell = ws.cell(row=row_idx, column=col_idx, value=value)
cell.alignment = Alignment(horizontal="left")
# Adjust column widths
for col in ws.columns:
max_length = 0
column = col[0].column_letter
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
ws.column_dimensions[column].width = max_length + 2
# Save the file
wb.save("Cleanup_Schedule.xlsx")
print("Created Cleanup_Schedule.xlsx successfully")
EOF
Step 3: Verify File Creation
After running the heredoc script, verify the file was created:
ls -lh *.xlsx
Step 4: Optional - Read Back to Confirm
Use read_file to confirm the Excel file is valid:
read_file with filetype="xlsx", file_path="Cleanup_Schedule.xlsx"
Key Advantages
| Aspect | execute_code_sandbox | run_shell heredoc |
|---|---|---|
| Reliability | May fail with openpyxl | More stable execution |
| Styling support | Limited | Full openpyxl support |
| File output | Via ARTIFACT_PATH | Direct file write |
| Debugging | Limited output | Full stdout/stderr |
Common Styling Patterns
Bold Headers with Colored Background
from openpyxl.styles import Font, PatternFill
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
header_font = Font(bold=True, color="FFFFFF", size=12)
cell = ws.cell(row=1, column=1, value="Header")
cell.fill = header_fill
cell.font = header_font
Alternating Row Colors
from openpyxl.styles import PatternFill
gray_fill = PatternFill(start_color="D9D9D9", end_color="D9D9D9", fill_type="solid")
for row in range(2, ws.max_row + 1):
if row % 2 == 0:
for col in range(1, ws.max_column + 1):
ws.cell(row=row, column=col).fill = gray_fill
Borders and Alignment
from openpyxl.styles import Border, Side, Alignment
thin_border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
for row in ws.iter_rows():
for cell in row:
cell.border = thin_border
cell.alignment = Alignment(horizontal="center", vertical="center")
Troubleshooting
Issue: File not created after heredoc execution
- Fix: Check stdout for Python errors; ensure working directory is correct
Issue: openpyxl not found
- Fix: Install with
pip install openpyxlbefore running her
常见问题
会先尝试沙箱吗?
会。指南要求先尝试沙箱,只有失败时才切换。
展示了哪些样式?
示例包括加粗文字、填充色、对齐方式和边框。
如何找到保存的文件?
示例在保存后会打印一个路径标记,因此可以找到输出文件。
详情
作者的原始描述
Create Excel files with openpyxl by falling back to run_shell with inline Python heredoc when execute_code_sandbox fails
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12875
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。