How to split merged Excel cells in Python?… here is a solution to the problem.
How to split merged Excel cells in Python?
I’m trying to split only merged cells in an Excel file that contains multiple worksheets as follows:
Note that there are partial/completely empty rows. The rows are not merged.
With openpyxl
, I found the merged range of cells in each worksheet with the following 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)
Printout:
['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']
Because I found the merged range of cells, I need to split the range and fill the corresponding rows like this:
How to split like this using openpyxl
? I am new to using this module. Any feedback is greatly appreciated!
Solution
You need to use the unmerge feature. Example:
ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4)