Python: Iteratively Writing to Excel Files -
python 2.7. i'm using xlsxwriter.
let's have mydict = {1: 'one', 2: 'two', 3: 'three'}
i need perform transformation on value , write result spreadsheet.
so write function create new file , put headers in there , formatting, don't close can write further next function.
then write function transforming dict values , writing them worksheet.
i'm noob when comes classes please forgive me if looks silly.
import xlsxwriter class readwritespreadsheet(object): def __init__(self, outputfile=none, writeworkbook=none, writeworksheet=none): self.outputfile = outputfile self.writeworksheet = writeworksheet self.writeworkbook = writeworkbook # function works fine def setup_new_spreadsheet(self): self.writeworkbook = xlsxwriter.workbook(self.outputfile) self.writeworksheet = self.writeworkbook.add_worksheet('my worksheet') self.writeworksheet.write('a1', 'test') # 1 not def write_data(self): # forget iterating through dict self.writeworksheet.write('a5', mydict[1]) x = readwritespreadsheet(outputfile='test.xlsx') x.setup_new_spreadsheet() x.write_data()
i get:
exception exception: exception('exception caught in workbook destructor. explicit close() may required workbook.',) in <bound method workbook.__del__ of <xlsxwriter.workbook.workbook object @ 0x00000000023fdf28>> ignored
the docs error due not closing workbook, if close can't write further...
how structure class workbook , worksheet setup_new_spreadsheet()
able written write_data()
?
the exception mentioned in question triggered when python realises not need use workbook more in rest of code , therefore decides delete memory (garbage collection). when doing so, realise haven't closed workbook yet , not have persisted excel spreadsheet @ on disk (only happen on close assume) , raise exception.
if had method close on class did: self.writeworkbook.close() , made sure call last not have error.
Comments
Post a Comment