简体   繁体   English

可以改进从Excel调用Python的运行时(通过xlwings RunPython)吗?

[英]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. 我编写了一个使用xlwings库的Python脚本,以便从特定的电子表格中获取函数输入。 The idea is for me to run this code from Excel (having imported the xlwings module as a VBA module). 我的想法是从Excel运行此代码(已将xlwings模块作为VBA模块导入)。 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. 我可以像这样运行它(即从Excel中,在VBA中使用RunPython命令),或者我可以在PyCharm中运行脚本时设置Mock Caller,这使我更容易输入函数输入。

This set-up works fine, and when running the script in PyCharm it takes about 2 seconds on average. 这个设置工作正常,当在PyCharm中运行脚本时,平均需要大约2秒。 However when I run it from Excel, it takes about 30 seconds on average (given the same set of data inputs both times). 但是,当我从Excel运行它时,平均需要大约30秒(两次都给出相同的数据输入集)。

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? 所以看起来Excel调用Python需要的时间相对较大 - 有没有人对如何加快这一点有任何建议? Is this roughly how long it's meant to take to run python from Excel? 这大概是从Excel运行python需要多长时间?

Thanks in advance 提前致谢

Currently, the default behaviour of xlwings is to fire up a new interpreter session everytime you call RunPython . 目前,xl​​wings的默认行为是每次调用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. 这自然带来了开销,虽然在现代系统上它通常只需要2-3秒而不是你看到的。
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 . 但是,在Windows上,您可以在VBA设置中切换OPTIMIZED_CONNECTION = True ,然后使用在两次调用之间保持运行的COM服务器,在文档中阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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