excel-unmerge-before-write
What it does
This skill explains how to handle merged cells in an Excel template before filling it with data using openpyxl. It is for people who fill pre-made spreadsheet forms with automation.
When to use it
- ✓When an Excel template has merged cells where I need to add values.
- ✓When writing to a cell gives an AttributeError.
- ✓When I need to fill in data in areas of a template that contain merged ranges.
Skill link
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/excel-unmerge-before-write/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Fill in my Excel template with the customer data, but the merged cells keep causing errors. Please fix that first.”
5 new skills a week. 1 minute to read.
How it works
The AI lists the merged ranges in the sheet, unmerges the ones in the write area, and then writes each value into the now-separate cells before saving.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
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
AttributeErrorwhen 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
Identify merged ranges in your target worksheet:
print(ws.merged_cells.ranges)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))Write your data to the now-unmerged cells:
ws.cell(row=46, column=1, value='Your data')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
| 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.rangesbefore 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
FAQ
What causes the error?
Writing to a merged cell can fail in openpyxl, so the merge must be removed first.
Can I unmerge only some ranges?
Yes, the guide says you can unmerge only the specific ranges you need.
Does it overwrite the template?
The example saves the filled workbook under a new file name.
Details
Original description by the author
Unmerge merged cells in openpyxl worksheets before writing values to avoid AttributeError
repo: HKUDS/AgentPilot
Popularity
- GitHub stars
- 7.8k
- Ranking position
- #12877
- License
- MIT
- Updated
- 11/1/2026
Comments
- …
More skills from this repository
agentpilot
7.8kHelps your agent improve a project step by step by preparing a task and running the AgentPilot loop that proposes, tests and scores changes.
openspace
7.8kHelps your agent set up OpenSpace, a tool that stores and fetches agent skills, and connect it to your AI app through MCP.
rsi-arena
7.8kHelps your agent set up and run standard AI benchmark tests, such as AIME and Terminal-Bench, to compare different improvement methods.
experiment-guide
7.8kHelps your agent plan small experiments, read their results and record findings while it investigates how to improve a program.
Similar skills
feishu-doc
394kHelps your agent read, create, and edit Feishu cloud documents, including text, images, and tables, using the document tool.
feishu-drive
394kLets your agent browse, organize, and comment on files in a Feishu cloud folder that has been shared with the bot.
feishu-perm
394kHelps your agent check and change who can access a Feishu file, choosing the smallest permission that meets your request.