CPU
CPU

Reputation: 287

How to split merged Excel cells with Python?

I am trying to split only the merged cells in Excel file (with multiple sheets) that are like:

Original sheets

Please note that there are partially/fully empty rows. These rows are not merged.

Using openpyxl, I found the merged cell ranges in each sheet with this code:

wb2 = load_workbook('Example.xlsx')  
sheets = wb2.sheetnames  ##['Sheet1', 'Sheet2']
for i,sheet in enumerate(sheets):
    ws = wb2[sheets[i]]
    print(ws.merged_cell_ranges)   

The print output:
['B3:B9', 'B13:B14', 'A3:A9', 'A13:A14', 'B20:B22', 'A20:A22']

['B5:B9', 'A12:A14', 'B12:B14', 'A17:A18', 'B17:B18', 'A27:A28', 'B27:B28', 'A20:A22', 'B20:B22', 'A3:A4', 'B3:B4', 'A5:A9']

Since I found the merged cell ranges, I need to split the ranges and fill in the corresponding rows like this:

Desired Sheets

How can I split like this using openpyxl? I am new to using this module. Any feedback is greatly appreciated!

Upvotes: 5

Views: 9892

Answers (2)

aka863
aka863

Reputation: 31

when you use unmerge_cells function, sheet.merged_cells.ranges will be modified, so don't use sheet.merged_cells.ranges in for loop.

from openpyxl import load_workbook
from openpyxl.utils.cell import range_boundaries

wb = load_workbook(filename = 'tmp.xlsx')

for st_name in wb.sheetnames:
    st = wb[st_name]
    mcr_coord_list = [mcr.coord for mcr in st.merged_cells.ranges]
    
    for mcr in mcr_coord_list:
        min_col, min_row, max_col, max_row = range_boundaries(mcr)
        top_left_cell_value = st.cell(row=min_row, column=min_col).value
        st.unmerge_cells(mcr)
        for row in st.iter_rows(min_col=min_col, min_row=min_row, max_col=max_col, max_row=max_row):
            for cell in row:
                cell.value = top_left_cell_value

wb.save('merged_tmp.xlsx')

Upvotes: 3

Pants
Pants

Reputation: 116

You need to use the unmerge function. Example:

ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4)

Upvotes: 3

Related Questions