PermissionError [WinError 32]:使用 xlwings 打开 excel 文件后,文件正在被另一个进程使用
PermissionError [WinError 32]: File is being used by another process after opening excel file with xlwings
描述
为了触发 excel 中某些公式字段的计算,然后使用 pandas 读取它们的值,我有以下脚本部分可以打开 [=53] 中的工作簿=],然后保存并关闭工作簿,并尝试删除打开工作簿时创建的临时文件:
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:
"""
# 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")
这个脚本 运行 在 Mac 上成功但是当我尝试 运行 它在 Windows 上时我得到以下错误似乎是由 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
其他上下文
- 我正在尝试 运行 使用 Windows Server 2019 Standard
在虚拟桌面上运行此脚本
- 我正在使用 python 3.7.7 和 xlwings 版本 0.21.4
- 我试图执行的脚本部分主要来自这个答案:
- 并且我根据本期的注释添加了
book.close()
:PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
- 我也无法再手动删除文件,大概是因为同样的问题
问题
- 是否有better/different强制关闭文件的方法?
- 此错误发生在 Windows 而不是 Mac 上是否有原因?
- 有没有一种 better/different 方法可以在不通过应用程序打开工作簿的情况下触发 excel 中的公式计算,这可能是导致此问题的原因
macOS 不会仅仅因为其他应用程序正在使用文件而阻止文件,因此即使您删除可能仍在应用程序中打开的文件,您也不会收到错误消息。
另一方面,Windows 不允许您在某处打开文件或文件夹后立即将其删除。
现在,在您的情况下,Python 想要删除文件,而 Excel 仍在关闭文件的过程中,这就是您收到错误的原因。
上次我运行进入这个问题,我是这样解决的:
import time
for attempt in range(5):
try:
temp_path.unlink()
break
# you may want to explicitly check for PermissionError
except Exception as e:
time.sleep(1)
关于你的最后一个问题:我不知道有任何 Python 库可以在没有 Excel 的情况下计算公式,但我相信在其他语言(例如 C#)中存在这样的库。
描述
为了触发 excel 中某些公式字段的计算,然后使用 pandas 读取它们的值,我有以下脚本部分可以打开 [=53] 中的工作簿=],然后保存并关闭工作簿,并尝试删除打开工作簿时创建的临时文件:
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:
"""
# 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")
这个脚本 运行 在 Mac 上成功但是当我尝试 运行 它在 Windows 上时我得到以下错误似乎是由 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
其他上下文
- 我正在尝试 运行 使用 Windows Server 2019 Standard 在虚拟桌面上运行此脚本
- 我正在使用 python 3.7.7 和 xlwings 版本 0.21.4
- 我试图执行的脚本部分主要来自这个答案:
- 并且我根据本期的注释添加了
book.close()
:PermissionError: [WinError 32] The process cannot access the file because it is being used by another process - 我也无法再手动删除文件,大概是因为同样的问题
问题
- 是否有better/different强制关闭文件的方法?
- 此错误发生在 Windows 而不是 Mac 上是否有原因?
- 有没有一种 better/different 方法可以在不通过应用程序打开工作簿的情况下触发 excel 中的公式计算,这可能是导致此问题的原因
macOS 不会仅仅因为其他应用程序正在使用文件而阻止文件,因此即使您删除可能仍在应用程序中打开的文件,您也不会收到错误消息。
另一方面,Windows 不允许您在某处打开文件或文件夹后立即将其删除。
现在,在您的情况下,Python 想要删除文件,而 Excel 仍在关闭文件的过程中,这就是您收到错误的原因。
上次我运行进入这个问题,我是这样解决的:
import time
for attempt in range(5):
try:
temp_path.unlink()
break
# you may want to explicitly check for PermissionError
except Exception as e:
time.sleep(1)
关于你的最后一个问题:我不知道有任何 Python 库可以在没有 Excel 的情况下计算公式,但我相信在其他语言(例如 C#)中存在这样的库。