Workbook context manager
Created originally on Bitbucket by Butla (Michał Bultrowicz)
From what I understand, right now after working with a workbook you have to manually close it (at least for read-only and write-only ones, from what I see in the code):
#!python
import openpyxl
workbook = openpyxl.load_workbook('some_workbook.xlsx')
# do some stuff with the workbook
workbook.close()
But what if I get an exception before calling close()
?
Wouldn't a context manager for workbooks be a good approach? So you can write something like this and don't worry?
#!python
with openpyxl.load_workbook_ctx_mgr('some_workbook.xlsx') as workbook:
# do some stuff with the workbook