1

I'm trying to access an excel workbook every minute to save data that it's currently displaying from a different program. When the scheduler gets to accessing the workbook, I'm getting "OSError: [WinError -2147467259] Unspecified error". Is there any fix/workaround? Any help would be appreciated, thanks!

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()

The full error is here

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
eric
  • 11
  • 2

1 Answers1

0

Not really a solution but rather an explanation. I think the issue is that APScheduler uses threading and xlwings objects can't be passed around directly in threads, see: http://docs.xlwings.org/en/stable/threading.html

It might be solvable with something like this: https://stackoverflow.com/a/27966218/918626 but currently nothing that is available out of the box with xlwings.

Felix Zumstein
  • 6,737
  • 1
  • 30
  • 62