Python xlwt单元格覆盖

时间:2017-12-14 09:52:19

标签: python excel xlwt

我试图解析列表"单词"到一列,但我有一个细胞覆盖问题。

import xlwt

wb=xlwt.Workbook()
sh=wb.add_sheet('sheet1')
sh.write(0,0,'words')

words = ["amazing","awesome","great"]
cols=['A']

for num in range(1,4):
     row = sh.row(num)
     for index,col in enumerate(cols):
             value = words[index]+str(num)
             row.write(index,value)   
wb.save('example.xls')

1 个答案:

答案 0 :(得分:0)

正如您对标题行所做的那样使用sh.write()。使用enumerate()提供行号,从1开始(参见enumerate()的第二个参数):

import xlwt

wb=xlwt.Workbook()
sh=wb.add_sheet('sheet1')
sh.write(0,0,'words')

words = ["amazing", "awesome", "great"]

for row, word in enumerate(words, start=1):
    sh.write(row, 0, word)
#    sh.write(row, 0, '{}{}'.format(word, row))    # use this if you want the row number appended

wb.save('example.xls')