How to detect merged cells in excel with openpyxl

前端 未结 4 2057
挽巷
挽巷 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:22

    To test if a single cell is merged, I loop through sheet.merged_cells.ranges like @A. Lau suggests. Unfortunately, checking the cell type like @0x4a6f4672 shows does not work any more.

    Here is a function that shows you how to do this.

    def testMerge(row, column):
        cell = sheet.cell(row, column)
        for mergedCell in sheet.merged_cells.ranges:
            if (cell.coordinate in mergedCell):
                return True
        return False
    

提交回复
热议问题