Save the contents of the table. If a cell is empty, it enters into the table None. How can this be intercepted, so that if None then nothing is recorded in the cell?

def save_file(self): filename = (QFileDialog.getSaveFileName(self, 'Сохранить', '', ".xls(*.xls)")) wbk = xlwt.Workbook() sheet = wbk.add_sheet("схема", cell_overwrite_ok=True) self.s_f(sheet) wbk.save(filename) def s_f(self, sheet): for currentColumn in range(self.table.columnCount()): for currentRow in range(self.table.rowCount()): my_icon = str(self.table.item(currentRow, currentColumn)) sheet.write(currentRow, currentColumn, my_icon) 
  • Probably not worth str() with everything called. Try this: if item is not None: sheet.write(...) - jfs
  • Without str swears. Tried anyway None prints. - Dr.robot
  • one
    You first check on None, and only then call str () (if the expected use of the API in this case). - jfs

2 answers 2

something like:

 my_icon = self.table.item(currentRow, currentColumn) sheet.write(currentRow, currentColumn, str(my_icon) if my_icon is not None else '') 
  • This is how it works. Thanks - Dr.robot
 sheet.write(currentRow, currentColumn, str(my_icon or '')) 
  • Yes, so clearer) - Dr.robot