I want to be able to read an Excel file in Python, keep the Python script running doing something else after the reading is finished, and be able to edit the Excel file in another process in the meantime. I'm using python 2.7 and openpyxl.
Currently it looks like:
from openpyxl import load_workbook
def get_excel_data():
OESwb = load_workbook(filename = OESconfigFile, data_only=True,
read_only=True)
ws = OESwb.get_sheet_by_name('MC01')
aValue = ws['A1'].value
return aValue
val = get_excel_data()
After I run the function, the Excel file is still locked for access from other processes (it gives the error "'filename' is currently in use. Try again later") even when I do not want to read it in Python anymore.
How can I close the file from my script? I've tried OESwb.close() but it gives the error "'Workbook' object has no attribute 'close'". I found this post but it doesn't seem to be helping.
EDIT: It appears OESwb.save('filename.xlsx') works, but only if read_only=False. However, it would be ideal to be able to close the file and still be in readonly mode. It appears this is a bug with openpyxl since it should close the file after load_workbook is finished.
Closing a file in Python Python has a close() method to close a file.
If you are working with large files or are particularly concerned about speed then you may find XlsxWriter a better choice than OpenPyXL. XlsxWriter is a Python module that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file.
openpyxl has a broader approval, being mentioned in *7 company stacks & 7 developers stacks; compared to xlrd, which is listed in 5 company stacks and 6 developer stacks.
Developers describe openpyxl as "A Python library to read/write Excel 2010 xlsx/xlsm files". A Python library to read/write Excel 2010 xlsx/xlsm files. On the other hand, pandas is detailed as "Powerful data structures for data analysis".
For some draconian reason, stackoverflow will allow me to post an answer but I don't have enough 'rep' to comment or vote -- so here we are.
The accepted answer of wb._archive.close()
did not work for me. Possibly this is because I am using read-only mode. It may work fine when in 'normal' mode.
bmiller's answer is the only answer that worked for me as well:
with open(xlsx_filename, "rb") as f:
in_mem_file = io.BytesIO(f.read())
wb = load_workbook(in_mem_file, read_only=True)
And as he said, it is faster when loading with open() versus only using read-only.
My working code based on bmiller's answer:
import openpyxl
import io
xlsx_filename=r'C:/location/of/file.xlsx'
with open(xlsx_filename, "rb") as f:
in_mem_file = io.BytesIO(f.read())
wb = openpyxl.load_workbook(in_mem_file, read_only=True)
I've tried all these solutions for closing an xlsx file in read-only mode and none seem to do the job. I finally ended up using an in-mem file:
with open(xlsx_filename, "rb") as f:
in_mem_file = io.BytesIO(f.read())
wb = load_workbook(in_mem_file, read_only=True)
Might even load faster and no need to worry about closing anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With