How to detect merged cells in excel with openpyxl

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

    These all helped (thanks), but when I used the approaches with a couple of spreadsheets, it wasn't unmerging all the cells I expected. I had to loop and restest for merges to finally get them all to complete. In my case, it took 4 passes to get everything to unmerge as expected:

        mergedRanges = sheet_ranges.merged_cells.ranges
        ### How many times do we run unmerge?
        i=0
        ### keep testing and removing ranges until they are all actually gone
        while mergedRanges:
            for entry in mergedRanges:
                i+=1
                print("  unMerging: " + str(i) + ": " +str(entry))
                ws.unmerge_cells(str(entry))
    

提交回复
热议问题