在excel中递增单元格:错误尝试覆盖单元格

时间:2018-03-20 10:46:52

标签: python excel python-2.7

我是excel-python的新手。我想从python中将值导出到excel。我有简单的代码如下。

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")

运行此代码后,我收到如下错误:

  

例外:尝试覆盖单元格:sheetname = u'表1'一行x = 9   colx = 0,print(x)打印x到2的值。

有人可以纠正我。

提前谢谢你。

2 个答案:

答案 0 :(得分:0)

你不需要第二个for循环,因为第一个循环它将循环直到9的范围结束。我没错?

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

答案 1 :(得分:0)

创建工作表时,您需要明确允许覆盖(默认情况下处于禁用状态),如下所示:

 `sheet1 = book.add_sheet ("sheet 1",cell_overwrite_ok=True)`