我正在尝试使用xlwings包将Python数据帧写入.xlsm
文件。我的代码有时工作正常,但有时会失败并抛出:
OSError: [WinError -2147467259] Unspecified error
示例代码:
import xlwings as xw
import pandas as pd
import numpy as np
app = xw.App(visible=False)
filename="test.xlsm"
book = xw.Book(filename)
sht = book.sheets("ASSET")
#asset_df ,asset_spec_df are the data-frame (i get this data-frame value from another function)
sht.range('B9').options(index=False, header=False).value = asset_df
sht=book.sheets("ASSETSPEC")
sht.range('B9').options(index=False, header=False).value = asset_spec_df
book.save()
book.close()
app.quit()
此代码有时会正常工作,但有时会抛出以下错误:
错误:
book = xw.Book(filename) --- error line
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 514, in __init__
for wb in app.books:
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 368, in books
return Books(impl=self.impl.books)
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 392, in books
return Books(xl=self.xl.Workbooks)
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 313, in xl
self._xl = get_xl_app_from_hwnd(self._hwnd)
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 223, in get_xl_app_from_hwnd
ptr = accessible_object_from_window(child_hwnd)
File "C:\Users\prabhaahar.nagesh\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 191, in accessible_object_from_window
res = oledll.oleacc.AccessibleObjectFromWindow(
File "_ctypes/callproc.c", line 948, in GetResult
OSError: [WinError -2147467259] Unspecified error
如注释中所述,如果一次在多个实例中打开同一本书(aka.
.xlsm
),应用程序将抛出错误检查您现在尝试运行的应用程序是否已经在后台运行。如果是,请关闭这些活动应用程序并重新运行
以下是有关此问题的更多信息->link
相关问题 更多 >
编程相关推荐