1

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.

2
  • (How) Did you install xlwt python module? Commented Apr 7, 2017 at 8:26
  • I did indeed (otherwise I wouldn't even have this error), and I used pip for this matter. Also worth noting, the column merging works. Commented Apr 7, 2017 at 8:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my God I just understood I was putting the parameters wrong from the start. Thanks a lot for your answer!
0

the arguments of write_merge method should be in the following order to work: sheet.write_merge(start_row,end_row,start_col,end_col,"Cells Value") ex:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.