简体   繁体   中英

Can the runtime of calling Python from Excel (via xlwings RunPython) be improved?

I have written a Python script which uses the xlwings library, in order to get the function inputs from a particular spreadsheet. The idea is for me to run this code from Excel (having imported the xlwings module as a VBA module). I can either run it like this (ie from Excel, using the RunPython command in VBA) or I can set a Mock Caller when running the script in PyCharm, which makes it easier for me to enter the function inputs.

This set-up works fine, and when running the script in PyCharm it takes about 2 seconds on average. However when I run it from Excel, it takes about 30 seconds on average (given the same set of data inputs both times).

So it looks like the time it takes Excel to call Python is relatively significant - does anyone have any advice on how to speed this up? Is this roughly how long it's meant to take to run python from Excel?

Thanks in advance

Currently, the default behaviour of xlwings is to fire up a new interpreter session everytime you call RunPython . This naturally comes with an overhead, although on modern systems it's usually just an additional 2-3 seconds rather than what you see.
On Windows you can however switch OPTIMIZED_CONNECTION = True in the VBA settings which will then use a COM server that keeps running in between calls, read more about it in the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM