Search code examples
pythonexcelpython-2.7xlwt

increment the cell in excel: error attempt to overwrite cell


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.


Solution

  • 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