Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unmerge cells in excel worksheet?

I have a class that represents a worksheet in Excel. I would like to get rid of all merged cells using unmerge_cells function. When I am running the code I can see (comparing to my excel worksheet) that all ranges from the list are passed correctly, but the range does not seem to be unmerged.. None of ranges have been unmerged.. What am I overseeing?

from openpyxl.utils import *


class ExcelSheet(object):
    """

    This class represents excel worksheet.
    Attributes:

        sheet_obj - excel worksheet object
        self.merged_cells_ranges - list of ranges with merged cells

    """
    def __init__(self, sheet_obj):
        self.sheet_obj = sheet_obj
        self.merged_cells_ranges = self.sheet_obj.merged_cell_ranges


    def unmerge_cells(self):
        if len(self.merged_cells_ranges) > 0:
            for range in self.merged_cells_ranges:
                self.sheet_obj.unmerge_cells(range)
like image 553
krakowi Avatar asked Jan 22 '26 00:01

krakowi


1 Answers

unmerge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None) 

range_string is a cell range (e.g. A1:E1)

You need to pass the range as a string.

like image 154
ConSod Avatar answered Jan 24 '26 21:01

ConSod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!