首页 > 解决方案 > python xlwings包错误:OSError:[WinError -2147467259]未指定错误

问题描述

.xlsm我正在尝试使用 xlwings 包将 Python 数据帧写入文件。我的代码有时可以正常工作,但有时会失败并抛出:

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

标签: pythonpython-3.xpandasxlwings

解决方案


.xlsm如评论中所写,如果一次在多个实例中打开同一本书(又名。),应用程序将引发错误。

检查您现在尝试运行的此应用程序是否已在后台某处运行。如果是这样,请终止这些活动的应用程序并再次运行。

这是有关该问题的更多信息->链接


推荐阅读