Description
In order to trigger the calculation of some formula fields in excel and then read in their values using pandas, I have the following portion of a script that opens the workbook in excel, then saves and closes the workbook and attempts to delete the temporary file created when the workbook is opened:
def df_from_excel(path):
"""Automates the opening and saving of an excel workbook before reading it
in through pd.read_excel, which addresses the NaN issue described in this
post: https://stackoverflow.com/a/41730454/7338319
"""
# open and save the wb to trigger formulas
app = xl.App(visible=False)
book = app.books.open(path)
book.save()
book.close()
app.kill()
# delete the temporary file that's created
temp_path = path.parent / ("~$" + str(path.name))
if temp_path.exists():
temp_path.unlink()
# read the excel into a dataframe
return pd.read_excel(path, engine="openpyxl")
This script runs successfully on Mac but when I try to run it on Windows I get the following error which seems to be raised by temp_path.unlink()
self = WindowsPath('C:/Users/william.daly/Documents/GitHub/prompt_payment/tests/run/~$CoreIntegrator.xlsx')
def unlink(self):
"""
Remove this file or link.
If the path is a directory, use rmdir() instead.
"""
if self._closed:
self._raise_closed()
> self._accessor.unlink(self)
E PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\william.daly\\Documents\\GitHub\\prompt_payment\\tests\\run\\~$CoreIntegrator.xlsx'
env\lib\pathlib.py:1304: PermissionError
Additional Context
- I am attempting to run this script on a virtual desktop with Windows Server 2019 Standard
- I'm using python 3.7.7 and xlwings version 0.21.4
- The portion of the script that I'm trying to execute was mainly pulled from this answer: https://stackoverflow.com/a/41730454/7338319
- And I added
book.close()
based on the notes in this issue: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process - I can also no longer delete the file manually, presumably because of the same issue
Questions
- Is there a better/different way to force the close of the file?
- Is there a reason this error occurs on Windows but not on Mac?
- Is there a better/different way to trigger the calculations of formulas in excel without opening the workbook via the app, which is what is presumably causing this issue