Can the runtime of calling Python from Excel (via xlwings RunPython) be improved?
<p>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 (i.e. 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.</p>

<p>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).</p>

<p>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?</p>

<p>Thanks in advance</p>

<p>Currently, the default behaviour of xlwings is to fire up a new interpreter session everytime you call <code>RunPython</code>. This naturally comes with an overhead, although on modern systems it's usually just an additional 2-3 seconds rather than what you see.<br>
On Windows you can however switch <code>OPTIMIZED_CONNECTION = True</code> in the VBA settings which will then use a COM server that keeps running in between calls, read more about it in the <a href="http://docs.xlwings.org/en/stable/vba.html#settings" rel="nofollow">docs</a>.</p>

python excel vba pycharm xlwings