Losing merged cells border while editing Excel file with openpyxl

后端 未结 5 1396
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 13:22

I have two sheets in an Excel file and the first one is a cover sheet which I don\'t need to edit. There are a few merged cells in the cover sheet, and when I edit the file usin

5条回答
  •  醉话见心
    2021-02-09 14:08

    For me this code work perfectly:

    wb = load_workbook(template_path)
    ws = wb.active
    for merged_cells in ws.merged_cells.ranges:
    style = ws.cell(merged_cells.min_row, merged_cells.min_col)._style
    for col in range(merged_cells.min_col, merged_cells.max_col + 1):
        for row in range(merged_cells.min_row, merged_cells.max_row + 1): 
            ws.cell(row, col)._style = style
    

    I simply find all merged cells and apply style of left top cell to the rest of the cells.

提交回复
热议问题