How to detect merged cells in excel with openpyxl

前端 未结 4 2029
挽巷
挽巷 2020-12-31 08:37

I\'m trying to read data from excel sheet that contains merged cells. When reading merged cells with openpyxl the first merged cell contain the value and the rest of the cel

4条回答
  •  时光说笑
    2020-12-31 09:21

    To test if a single cell is merged or not you can check the class (name):

    cell = sheet.cell(row=15, column=14)
    if type(cell).__name__ == 'MergedCell':
      print("Oh no, the cell is merged!")
    else:
      print("This cell is not merged.")
    

    To "unmerge" all cells you can use the function unmerge_cells

    for items in sorted(sheet.merged_cell_ranges):
      print(items)
      sheet.unmerge_cells(str(items))
    

提交回复
热议问题