Reputation: 35
I have a problem with merging cells in python:
def merge_cells_if_value(cell, cell_row, sheet, row_with_names_index, nested_row_index):
pre_up_cell_row = cell_row - 1
cell_column_letter = COLUMN_LETTERS[cell.column - 1]
pre_up_cell_coords = f'{cell_column_letter}{pre_up_cell_row}'
cur_cell_coords = f'{cell_column_letter}{cell.row}'
if sheet[pre_up_cell_coords].value is not None or pre_up_cell_row == nested_row_index:
if pre_up_cell_row != row_with_names_index:
if sheet[pre_up_cell_coords].value is None:
sheet[f'{pre_up_cell_coords}'] = ''
print(cell, pre_up_cell_coords, sheet[pre_up_cell_coords].value, nested_row_index)
sheet.merge_cells(
f'{pre_up_cell_coords}:'
f'{cur_cell_coords}'
)
target_cell = sheet[f'{pre_up_cell_coords}']
else:
target_cell = cell
make_cell_alignment(target_cell, wrap_text=True)
make_cell_border(target_cell)
else:
merge_cells_if_value(cell, pre_up_cell_row, sheet, row_with_names_index, nested_row_index)
`
I using library openpyxl and when open excel file sample.xlsx I get the next error: "We found a problem with some content in 'filename.xlsx'. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click Yes".
Removed Records: Merge cells from /xl/worksheets/sheet0.xml part
I tried to fill all cells with, at least, empty string (''), and I know, that there isn't any cell with None value
Upvotes: 0
Views: 280
Reputation: 11
Try to merge before fetching the table data. You can merge cells first and then generate data. I solved the problem by doing.
Upvotes: 1