源码

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 …

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-jiangqtablewidgetxieru-csvhuo-xls.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 25, 2019 at 05:05 下午

热评文章