As you want to copy the formatting of some cells in excel, you need to select the merged cells that meet the conditions of the
The following example is a reservation of less than 15
Then in the new form
wbsheet_new.merge_cells(cell2)
wbsheet_new for the new form, cell2 for the retention of filtered cells, expressed as I24: J24, K24: L24 such a format
First regular filtering, the result of the filtering is [('AO', 'AP')] this list contains tuple expression, and then extract the first element with result[0][0].
If greater than 15 columns
column_index_from_string(result[0][0])>=15
Remove them and keep the rest
from import get_column_letter, column_index_from_string import re s=['AK23:AL23', 'AM23:AN23', 'AO23:AP23', 'AQ23:AR23', 'B24:C24', 'D24:F24', 'G24:H24', 'I24:J24', 'K24:L24', 'M24:N24', 'Q24:R24', 'S24:U24', 'V24:W24', 'X24:Y24', 'Z24:AA24', 'AB24:AC24', 'AF24:AG24', 'AH24:AJ24', 'AK24:AL24', 'AM24:AN24', 'AO24:AP24',] for si in s: result=re._compile(r'(\w+?)\d+:(\w+?)\d+',flags=0).findall(si) print (result) if column_index_from_string(result[0][0])>=15: print('Remove %s'%result[0][0])
Addendum: python openpyxl get list of merged cells, filter unmerge cells
The code is as follows:
# Get information about all merged cells in the table and iterate through them. for i in ws.merged_cell_ranges: # Get the starting row and column of the merged cell, and the ending row and column r1, r2, c1, c2 = i.min_row, i.max_row, i.min_col, i.max_col # Unconsolidation ws2.unmerge_cells(start_row = r1, end_row = r2, start_column = c1, end_column = c2)
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.