姬長信(Redy)

python – 将QTableWidget写入.csv或.xls


是否可以将QTableWidget的内容写入csv?我发现了一个关于使用xlwt写入.xls的问题,但似乎无法使用我的代码使其工作.
def saveFile(self):
    filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
    wbk = xlwt.Workbook()
    self.sheet = wbk.add_sheet("sheet")
    self.write()
    wbk.save(filename)    


def write(self):
    for col in range (self.coordinates.columnCount()):
        for row in range(self.coordinates.rowCount()):
            text=str(self.coordinates.item(row,col).text())
            self.sheet.write(row,col,text)

我收到以下错误:

  File "C:/Users/Tory/Desktop/DIDSON.py", line 186, in saveFile
    self.write()
  File "C:/Users/Tory/Desktop/DIDSON.py", line 192, in write
    text=str(self.coordinates.item(row,col).text())
AttributeError: 'NoneType' object has no attribute 'text'

为什么是这样? self.coordinates是一个QTableWidget.我能够将项目本身成功保存到工作表中,虽然我仍然想保存为.csv …