I use a self-written script to upload data from a legacy product through ods documents.
Current implementation:
import ezodf from . import db ezodf.config.set_table_expand_strategy('all') current_spreadsheet = ezodf.opendoc(filename='./table1.ods') current_table = current_spreadsheet.sheets[-1] for row_num in range(current_table.nrows()): db.session.add( Model_1( id=current_table['a%s' % row_num].value, name=current_table['b%s' % row_num].value ) ) db.session.commit() So, as the tables are quite a decent amount and they have a different structure, I would like to brush the code like this:
with ezodf.opendoc(filename='./table1.ods') as spreadsheet: with spreadsheet.sheets[-1] as table: for row_num in range(table.nrows()): db.session.add( Model_1( id=table['a%s' % row_num].value, name=table['b%s' % row_num].value ) ) But when performing the second option, I get the following:
File "uploader.py", line 36, in <module> with ezodf.opendoc(filename='./table_1.ods') as spreadsheet: AttributeError: __exit__ How to get around / catch this error?
ezodf - 0.3.2 lxml - 3.7.2 python - 3.5