increment the cell in excel: error attempt to overwrite cell

Question:

I am new in excel-python. I wanted to export values in to excel from python. I have simple code as below.

import xlwt
book = xlwt.Workbook (encoding = "utf-8")
sheet1 = book.add_sheet ("sheet 1")
sheet1.write(0,0,"Display")
x = 1
m = 1
for x in range (1,9):
    sheet1.write (m,0,x)
    print (x)
    x = x+1
for m in range (1,9):
    m = m +1 
    book.save("trial.xls")

after running this code i am getting errors like:

Exception: attempt to overwrite cell: sheetname= u’sheet 1′ rowx=9
colx = 0 and print (x) is printing the values of x till 2.

Can some one correct me.

Thank you in advance.

Asked By: Ravi Mevada

||

Answers:

You don’t need the second for loop, because the first one will loop it until the range of 9 ends. I’m right?

for x in range (1,9):
    sheet1.write (m,0,x)
    print (x)
    x = x+1
    m = m +1
Answered By: Ricardo Marques

When you create a sheet, you need to explicitly allow overwriting (it’s disabled by default) as below:

sheet1 = book.add_sheet ("sheet 1",cell_overwrite_ok=True)
Answered By: paullb
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.