‹ 首页

excel-unmerge-before-write

@hkuds · 收录于 5 天前 · 上游提交 1 周前

Unmerge merged cells in openpyxl worksheets before writing values to avoid AttributeError

适合你,如果经常用openpyxl操作合并单元格的Excel文件

/ 通过 npx 安装 校验哈希
npx oh-my-skill add hkuds/openspace/excel-unmerge-before-write
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- hkuds/openspace/excel-unmerge-before-write
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify hkuds/openspace/excel-unmerge-before-write
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
6920GitHub stars
~664上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 2c5cc40 · 表述以原文为准
它做什么

当 Claude 编写 Python 代码处理 Excel 时,它会在写入数据前自动取消合并单元格,避免因向合并单元格写入数据而报错。

什么时候触发

当你要求 Claude 编写代码填充带有合并单元格的 Excel 模板,或在处理 Excel 时遇到 AttributeError 时触发。

装好后可以这样说
Claude 会生成一个先取消合并再写入数据的代码。
技能原文 SKILL.md作者撰写 · MIT · 2c5cc40

Excel Unmerge Before Write

When automating Excel file population with openpyxl, merged cells in templates can cause write failures. This skill provides the pattern to safely handle merged ranges before populating data.

When to Use
  • Working with Excel templates that have pre-existing merged cells
  • Getting AttributeError when trying to write values to certain cells
  • Needing to populate data in areas that may contain merged ranges
Core Pattern

Before writing values to cells in a worksheet, identify and unmerge any overlapping ranges:

from openpyxl import load_workbook

# Load the workbook
wb = load_workbook('template.xlsx')
ws = wb.active

# Unmerge specific ranges before writing
ws.unmerge_cells('A46:C46')
ws.unmerge_cells('E46:F46')
ws.unmerge_cells('I46:K46')

# Now safely write values
ws['A46'] = 'Value 1'
ws['E46'] = 'Value 2'
ws['I46'] = 'Value 3'

wb.save('output.xlsx')
Step-by-Step Instructions
  1. Identify merged ranges in your target worksheet: ```python print(ws.merged_cells.ranges) ```
  1. Unmerge relevant ranges before writing any data to those areas: ```python for merged_range in ws.merged_cells.ranges: # Optionally filter by area if you only need specific ranges ws.unmerge_cells(str(merged_range)) ```
  1. Write your data to the now-unmerged cells: ```python ws.cell(row=46, column=1, value='Your data') ```
  1. Save the workbook: ```python wb.save('output.xlsx') ```
Handling Multiple Worksheets

If your workbook has multiple sheets with merged cells:

for sheet_name in wb.sheetnames:
    ws = wb[sheet_name]
    for merged_range in list(ws.merged_cells.ranges):
        ws.unmerge_cells(str(merged_range))
Common Errors

| Error | Cause | Solution | |-------|-------|----------| | AttributeError on cell write | Writing to merged cell | Call unmerge_cells() first | | KeyError on range | Invalid range string | Use exact range format like 'A1:B2' | | Data overwrites neighbors | Unmerged too broadly | Unmerge only needed ranges |

Best Practices
  • Unmerge early: Call unmerge_cells() immediately after loading the worksheet, before any write operations
  • List before unmerging: Capture ws.merged_cells.ranges before unmerging if you need to know what was merged
  • Preserve formatting: If merge was for visual formatting, consider re-applying merges after data population if needed
  • Test ranges: Verify unmerged ranges don't break template layout expectations
Complete Example
from openpyxl import load_workbook

def populate_excel_template(template_path, output_path, data_dict):
    """Populate an Excel template, handling merged cells safely."""
    wb = load_workbook(template_path)
    ws = wb.active
    
    # Unmerge all cells that might conflict with data writes
    for merged_range in list(ws.merged_cells.ranges):
        ws.unmerge_cells(str(merged_range))
    
    # Populate data
    for cell_ref, value in data_dict.items():
        ws[cell_ref] = value
    
    wb.save(output_path)
    return output_path
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。