1

I have a Python script which a few people should be running which uses XLWings to read in live data from a sheet:

book = xw.Book(path + "book_to_read.xlsm")

This works fine on my machine and on some others, but seems to be consistently throwing the same error for most users, "Call was rejected by callee."

I understand that Excel will throw this error if it's being given "too much" to do but that doesn't explain why the bug is only appearing for some people

As far as I can tell everyone is using the same version of Python (3.10), the same version of Excel (2016 64-bit), and has all the relevant dependencies installed. The only difference as far as I can tell is the choice of IDE but I don't see how this could be relevant.

Clearly I'm being stupid and missing something obvious but would appreciate pointers on what might be inconsistent between instances

EDIT: It seems the issue is only occurring for users with multiple Excel books open with a lot of work/macros. Is there a way to work-around this?

addawaddawoo
  • 47
  • 1
  • 5
  • Does this answer your question? [how to solve Exception:Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC\_E\_CALL\_REJECTED)) in C#?](https://stackoverflow.com/questions/20514548/how-to-solve-exceptioncall-was-rejected-by-callee-exception-from-hresult-0x8) – Chillie Nov 17 '21 at 15:49
  • Thanks - I had gone through that and wasn't able to find any suggestions that helped. By trial and error though we seem to have narrowed it down to breaking for users who already have a number of busy sheets open, so I supposed Excel is just getting overloaded and rejecting calls? If so, is there a workaround for this? – addawaddawoo Nov 18 '21 at 07:28
  • It really depends on what you want to do after reading the data. Using a library that doesn not use excel but can read/write its files is something you can try. I use `openpyxl` for such tasks. But, again, it's hard to recommend a solution without more details. – Chillie Nov 19 '21 at 09:17

1 Answers1

0

Xlwings is not able to connect to an excel file if this excel file (or even another one) is currently being edited or is calculating. This means that if you have many heavy files computing in the background, xlwings cannot start.

If a file is being edited, the program is simply frozen while excel is busy and will continue when edition is done. On the other hand, if the file is busy computing the error Call was rejected by callee is triggered.

The error happens during the creation of the Book instance:

    import xlwings as xw

    wb = xw.Book(wb_folder + EXCEL_FILENAME)

A possible workaround is to surround this call by a try/catch. It will try to open the file 1000 times and stop the script if it was never able to do it. As soon as it is successfully opened, continue as usual:

    import xlwings as xw

    wb = None
    t = 1000
    while t > 0 and wb is None:
        try:
            wb = xw.Book(wb_folder + EXCEL_FILENAME)
        except Exception as str_error:
            print(t, str_error)
            t -= 1

In order to give more time to excel to finish its computation, you can also add a time.sleep(duration_in_ms) inside the loop.

When connected, my first action is to set the calculation mode to manual:

    wb.app.calculation = 'manual'
    [...]
    wb.app.calculation = 'automatic'

I put it back to automatic at the end of the script. The issue is that it can take a while before being able to actually connect to the file. If all files are set to manual before running the script, the issue never happens. Unfortunately, the users have massive excel file with many cells recomputed every seconds so these files are permanently recomputing.

So far, I didn't find a better way to avoid this issue.

Computer Specs:

  • Windows 10
  • Python 3.7.3
  • Excel Version 2107 (Build 14228.20250) from Microsoft 365
  • xlwings 0.23.3
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Romain Capron
  • 1,565
  • 1
  • 18
  • 23