gpt4 book ai didi

python - 通过 APScheduler 作为计划任务运行时,XLWings 无法连接到 Excel 工作簿

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:00 25 4
gpt4 key购买 nike

我试图每分钟访问 Excel 工作簿以保存当前从不同程序显示的数据。当调度程序访问工作簿时,我收到“OSError:[WinError -2147467259]未指定错误”。有任何修复/解决方法吗?任何帮助将不胜感激,谢谢!

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.cron import CronTrigger
import xlwings as xw

def tick():
wb= xw.Book('currently_open_workbook.xlsx')


sched = BlockingScheduler()

trigger = OrTrigger([
CronTrigger(day_of_week='mon-fri', hour='0-16', second=0),
CronTrigger(day_of_week='sun', hour='17-23', second=0)
])

sched.add_job(tick, trigger)

sched.start()

完整的错误在这里

Traceback (most recent call last):
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "C:/Users/eric/PycharmProjects/untitled/blank.py", line 8, in tick
wb= xw.Book('currently_open_workbook.xlsx')
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\main.py", line 472, in __init__
for wb in app.books:
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\main.py", line 358, in books
return Books(impl=self.impl.books)
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\_xlwindows.py", line 374, in books
return Books(xl=self.xl.Workbooks)
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\_xlwindows.py", line 302, in xl
self._xl = get_xl_app_from_hwnd(self._hwnd)
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\_xlwindows.py", line 218, in get_xl_app_from_hwnd
ptr = accessible_object_from_window(child_hwnd)
File "C:\Users\eric\anaconda3\envs\untitled\lib\site-packages\xlwings\_xlwindows.py", line 189, in accessible_object_from_window
byref(IDispatch._iid_), byref(ptr))
File "_ctypes/callproc.c", line 918, in GetResult
OSError: [WinError -2147467259] Unspecified error

最佳答案

这并不是真正的解决方案,而是一种解释。我认为问题在于 APScheduler 使用线程,并且 xlwings 对象无法直接在线程中传递,请参阅:http://docs.xlwings.org/en/stable/threading.html

它可能可以用这样的东西来解决:https://stackoverflow.com/a/27966218/918626但目前 xlwings 没有任何开箱即用的功能。

关于python - 通过 APScheduler 作为计划任务运行时,XLWings 无法连接到 Excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com