'"Call was rejected by callee" error in XLWings for some users but not others
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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Aya |
