vba 在excel上按下按钮时如何执行python脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6688115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:39:23  来源:igfitidea点击:

How to excute python script when pushing a button on excel

pythonexcelvba

提问by riscy

I have several python scripts, test1,py, test2,py, test3.py and so on.

我有几个 python 脚本,test1,py, test2,py, test3.py 等等。

I would like to have three buttons (using VBA) on the Excel screen.

我想在 Excel 屏幕上有三个按钮(使用 VBA)。

How do I associate a button to a specific python filename so that I can execute the file and capture the python response into a text box within excel (message response).

如何将按钮与特定的 python 文件名相关联,以便我可以执行该文件并将 python 响应捕获到 excel 中的文本框中(消息响应)。

回答by Michael Dillon

You could try embedding Python in Excel with this http://code.google.com/p/pyinex/

您可以尝试使用此http://code.google.com/p/pyinex/在 Excel 中嵌入 Python

Or just have some VBA code that runs a Python shell script and captures the output into a file, then reads the file and fills the textbox.

或者只是有一些运行 Python shell 脚本并将输出捕获到文件中的 VBA 代码,然后读取文件并填充文本框。

Or try writing a Python COM server in 20 lines https://web.archive.org/web/20111113152839/http://showmedo.com/videotutorials/series?name=ktn7wmxvn

或者尝试用 20 行写一个 Python COM 服务器https://web.archive.org/web/20111113152839/http://showmedo.com/videotutorials/series?name=ktn7wmxvn

Or add Python as an Excel scripting language https://web.archive.org/web/20110718231808/http://blog.gobansaor.com/category/microetl/

或者添加 Python 作为 Excel 脚本语言https://web.archive.org/web/20110718231808/http://blog.gobansaor.com/category/microetl/

Once you decide how to connect Excel and Python, the rest is straightforward.

一旦决定了如何连接 Excel 和 Python,剩下的就很简单了。