科研技能库/Excel Heredoc 备选方案
科研效率
未发现用户侧风险

Excel Heredoc 备选方案

当 execute_code_sandbox 环境创建 Excel 文件失败时,回退到使用 run_shell 和内联 Python heredoc 来生成带样式的电子表格工作簿。提供完整的代码模板与故障排除指南。

文件预览

2 个文件
SKILL.md
6.0 KB · 可预览
---
name: excel-heredoc-workaround
description: Create Excel files with openpyxl by falling back to run_shell with inline Python heredoc when execute_code_sandbox fails
---

# 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 `.xlsx` files with openpyxl
- `execute_code_sandbox` fails 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`:

```python
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:

```bash
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:

```bash
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

```python
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

```python
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

```python
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 openpyxl` before running heredoc

**Issue**: Styling not appearing
- **Fix**: Ensure you save the workbook after applying all styles

## Example Complete Workflow

```bash
# Create Excel with full styling
python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side

wb = Workbook()
ws = wb.active
ws.title = "Report"

# Header row
headers = ["ID", "Name", "Value", "Date"]
for col, h in enumerate(headers, 1):
    cell = ws.cell(row=1, column=col, value=h)
    cell.font = Font(bold=True, color="FFFFFF")
    cell.fill = PatternFill(start_color="2F5597", fill_type="solid")
    cell.alignment = Alignment(horizontal="center")

# Data
ws.append([1, "Item A", 100, "2024-01-01"])
ws.append([2, "Item B", 200, "2024-01-02"])

# Apply borders
thin = Side(style='thin')
border = Border(left=thin, right=thin, top=thin, bottom=thin)
for row in ws.iter_rows():
    for cell in row:
        cell.border = border

# Auto-width columns
for col in ws.columns:
    col_letter = col[0].column_letter
    max_len = max(len(str(cell.value)) if cell.value else 0 for cell in col)
    ws.column_dimensions[col_letter].width = min(max_len + 2, 50)

wb.save("Report.xlsx")
print("Success: Report.xlsx created")
EOF
```

SKILL.md

元数据
nameexcel-heredoc-workaround
description当 execute_code_sandbox 失败时,回退到使用 run_shell 和内联 Python heredoc 创建 Excel 文件。

Excel Heredoc 备选方案

目的

当使用 openpyxl 创建 Excel 文件时,execute_code_sandbox 可能因环境问题而失败。此技能提供了一个稳健的备选方案:回退到 run_shell,通过内联 Python heredoc 脚本创建格式正确的电子表格,并支持样式设置。

适用场景

  • 需要使用 openpyxl 创建 .xlsx 文件
  • execute_code_sandbox 因 openpyxl 相关错误而失败
  • 需要样式、格式或复杂的电子表格功能
  • 可使用 shell 直接执行 Python heredoc

分步说明

步骤 1:首先尝试 execute_code_sandbox

尝试使用 execute_code_sandbox 创建 Excel 文件:

python
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill

wb = Workbook()
ws = wb.active
ws.title = "Data"

# 添加数据和样式
ws['A1'] = "Header"
ws['A1'].font = Font(bold=True)

wb.save("output.xlsx")
print("ARTIFACT_PATH:output.xlsx")

步骤 2:若沙盒失败,使用 run_shell 与 Heredoc

当 execute_code_sandbox 失败时,切换至 run_shell 并配合 Python heredoc:

bash
python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side

# 创建工作簿
wb = Workbook()
ws = wb.active
ws.title = "Schedule"

# 添加带样式的表头
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")

# 添加数据行
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")

# 调整列宽
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

# 保存文件
wb.save("Cleanup_Schedule.xlsx")
print("Created Cleanup_Schedule.xlsx successfully")
EOF

步骤 3:验证文件创建

运行 heredoc 脚本后,确认文件已创建:

bash
ls -lh *.xlsx

步骤 4:可选——回读确认

使用 read_file 确认 Excel 文件有效:

text
read_file with filetype="xlsx", file_path="Cleanup_Schedule.xlsx"

关键优势

方面execute_code_sandboxrun_shell heredoc
稳定性可能因 openpyxl 失败执行更稳定
样式支持有限完整 openpyxl 支持
文件输出通过 ARTIFACT_PATH直接写入文件
调试输出有限完整 stdout/stderr

常用样式模式

带彩色背景的粗体表头

python
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

交替行颜色

python
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

边框与对齐

python
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")

故障排除

问题:heredoc 执行后未生成文件

  • 修复:检查 stdout 中的 Python 错误;确保工作目录正确

问题:找不到 openpyxl

  • 修复:在运行 heredoc 前用 pip install openpyxl 安装

问题:样式未出现

  • 修复:确保在保存工作簿之前应用所有样式

完整工作流程示例

bash
# 创建带完整样式的 Excel
python3 << 'EOF'
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side

wb = Workbook()
ws = wb.active
ws.title = "Report"

# 表头行
headers = ["ID", "Name", "Value", "Date"]
for col, h in enumerate(headers, 1):
    cell = ws.cell(row=1, column=col, value=h)
    cell.font = Font(bold=True, color="FFFFFF")
    cell.fill = PatternFill(start_color="2F5597", fill_type="solid")
    cell.alignment = Alignment(horizontal="center")

# 数据
ws.append([1, "Item A", 100, "2024-01-01"])
ws.append([2, "Item B", 200, "2024-01-02"])

# 应用边框
thin = Side(style='thin')
border = Border(left=thin, right=thin, top=thin, bottom=thin)
for row in ws.iter_rows():
    for cell in row:
        cell.border = border

# 自动调整列宽
for col in ws.columns:
    col_letter = col[0].column_letter
    max_len = max(len(str(cell.value)) if cell.value else 0 for cell in col)
    ws.column_dimensions[col_letter].width = min(max_len + 2, 50)

wb.save("Report.xlsx")
print("Success: Report.xlsx created")
EOF