Chuyển đến nội dung
Skillpedia
PDF, Word và slide7.8k · #12877

excel-unmerge-before-write

Skill này làm gì

Skill này giải thích cách xử lý ô gộp trong mẫu Excel trước khi điền dữ liệu bằng openpyxl. Dành cho người điền các biểu mẫu bảng tính có sẵn bằng tự động hóa.

Khi nào nên dùng

  • ✓Khi một mẫu Excel có ô gộp và tôi cần thêm giá trị vào đó.
  • ✓Khi ghi vào một ô gây ra lỗi AttributeError.
  • ✓Khi tôi cần điền dữ liệu vào các vùng của mẫu có phạm vi gộp.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/excel-unmerge-before-write/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/excel-unmerge-before-write/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hãy điền dữ liệu khách hàng vào mẫu Excel của tôi. Các ô gộp liên tục gây lỗi, nên hãy sửa lỗi đó trước.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

AI liệt kê các phạm vi gộp trong trang tính, bỏ gộp những phạm vi nằm trong vùng ghi, rồi ghi từng giá trị vào các ô đã tách riêng trước khi lưu.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

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
Câu hỏi thường gặp

Điều gì gây ra lỗi?

Ghi vào một ô gộp có thể thất bại trong openpyxl, nên phải bỏ gộp trước.

Tôi có thể chỉ bỏ gộp một số phạm vi không?

Có. Hướng dẫn nói bạn có thể chỉ bỏ gộp những phạm vi cụ thể cần dùng.

Có ghi đè mẫu không?

Ví dụ lưu sổ tính đã điền dưới một tên tệp mới.

Chi tiết

Mô tả gốc của tác giả

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

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12877
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự