python - 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
解决方案
.xlsm
如评论中所写,如果一次在多个实例中打开同一本书(又名。),应用程序将引发错误。
检查您现在尝试运行的此应用程序是否已在后台某处运行。如果是这样,请终止这些活动的应用程序并再次运行。
这是有关该问题的更多信息->链接
推荐阅读
- javascript - useState 本身始终是默认值
- c# - 检查范围是否为空或为空
- javascript - 如何通过自动填写表单将数据从一页发送到另一页
- mysql - 在 AWS Redshift 中复制特殊字符
- python - 如何找到带下标的单词?
- jquery - 调整窗口大小后猫头鹰轮播幻灯片消失
- python - 如何将元组列表复制到共享内存中的现有 numpy 数组
- ffmpeg - ffmpeg - 文本字体颜色因 alpha 而改变(褪色)(我认为)。有没有可能避免这种行为?
- r - 从同一对象中调用 S4 函数
- azure-language-understanding - 如何拥有多个相同问题但答案不同的常见问题解答(KB)?