Search code examples
pythonxlwt

AssertionError while merging cells with xlwt (Python)


I'm using xlwt to do some Excel sheets generation, but it appears that whenever I try to merge cells in line, Python throws an AssertionError, even for a code as simple as:

import xlwt

wb = xlwt.Workbook(encoding = 'latin-1')
ws = wb.add_sheet('hey')

ws.write_merge(0,0,8,0,"hi")

wb.save("test.xls")

Could anyone please help me on this? Did I miss something? Thank you ever so much.


Solution

  • TLDR;

    Perhaps your call should be:

    ws.write_merge(0,0,0,8,"hi")
    

    Explaination: Fuller stack trace:

    AssertionError                            Traceback (most recent call last)
    <ipython-input-6-c6b20b7b1b27> in <module>()
    ----> 1 ws.write_merge(0,0,8,0)
    
    /usr/local/lib/python2.7/dist-packages/xlwt/Worksheet.pyc in write_merge(self, r1, r2, c1, c2, label, style)
       1110 
       1111     def write_merge(self, r1, r2, c1, c2, label="", style=Style.default_style):
    -> 1112         assert 0 <= c1 <= c2 <= 255
       1113         assert 0 <= r1 <= r2 <= 65535
       1114         self.write(r1, c1, label, style)
    

    I believe (I'm not sure but please correct me if I am wrong here) write_merge is used to merge multiple cells (portion of worksheet) into a single cell. The arguments are (I guess!) start_row, end_row, start_col, end_col resp. It seems natural to me that c1 <= c2 and r1 <= r2 must hold true to have a real portion to merge into a single cell.