本文へ移動
cccskills
無料GitHub で公開

excel-unmerge-before-write

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

インストール方法を見る

含まれるファイル(2)

  • SKILL.md3.2 KB
  • .skill_id40 B

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

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:

    print(ws.merged_cells.ranges)
    
  2. Unmerge relevant ranges before writing any data to those areas:

    for merged_range in ws.merged_cells.ranges:
        # Optionally filter by area if you only need specific ranges
        ws.unmerge_cells(str(merged_range))
    
  3. Write your data to the now-unmerged cells:

    ws.cell(row=46, column=1, value='Your data')
    
  4. Save the workbook:

    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

ErrorCauseSolution
AttributeError on cell writeWriting to merged cellCall unmerge_cells() first
KeyError on rangeInvalid range stringUse exact range format like 'A1:B2'
Data overwrites neighborsUnmerged too broadlyUnmerge 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

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Incremental audio production with duration mismatch handling, adaptive stem extension, and pre-mix alignment verification

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

Incremental audio production with duration alignment handling, per-stem verification, and adaptive extension strategies

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

Create serverless API proxy endpoints that hide API keys and provide a unified backend for the dashboard frontend. Designed for Vercel deployment.

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

End-to-end audio production workflow with stems, effects, archiving, and verification

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

Handle cascading data retrieval tool failures by falling back to embedded knowledge generation

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

Fallback pattern for executing Python code when execute_code_sandbox fails

日本語の概要は準備中です。原文の説明を表示しています。

HKUDS/OpenSpace7,7562026年8月13日 更新

HKUDS のスキルをすべて見る

このスキルの問題を報告する