如何在excel vba上调用python脚本? [英] How to call python script on excel vba?

查看:411
本文介绍了如何在excel vba上调用python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在Vba上调用一个python脚本,我是一个新手。我尝试使用py2exe将主脚本转换为exe,然后从VBA(shell)调用它,但主脚本调用其他脚本,因此它变得复杂,我搞砸了(我的exe不起作用)。此外,主脚本是一个大文件,我不想修改它。



底线是否有一种从excel vba调用主脚本的方法,而不将脚本转换为exe文件。



到目前为止,我试过:

  RetVal = Shell C:\python27\python.exe&import&C:\\&MainScriptFile)

它启动python.exe但没有别的。然后我试过:

  RetVal = Shell(C:\Windows\System32\cmd.exe& python&C:\\Python27\\hello.py)

它启动命令提示符,但甚至不启动python。



PS我在论坛上查了所有相关问题,他们并不解决我的疑问。

解决方案

尝试这样:

  RetVal = Shell(< python.exe的完整路径>&<你的python脚本的完整路径>)

或者如果python脚本与工作簿位于同一个文件夹中,则可以尝试:

  RetVal = Shell(<完整路径python.exe>& ActiveWorkBook.Path&\< python脚本名称>)

给予<> 中的所有详细信息。 <> - 表示可变字段



我想这应该工作。但是再一次,如果您的脚本将调用不同文件夹中的其他文件,除非您的脚本已正确处理,否则可能会导致错误。希望有帮助。


trying to call a python script on Vba and I am a newb. I tried converting the main script to an exe using py2exe and then calling it from VBA (shell) but the main script calls other scripts therefore it becomes complicated and I messed it up (my exe is not functional). Besides, the the main script is a large file and I do not want to revise it a lot.

Bottomline, is there a way to call the main script from excel vba, without converting the script to an exe file.

So far, I tried:

RetVal = Shell("C:\python27\python.exe " & "import " & "C:\\" & "MainScriptFile")

It starts python.exe but does nothing else. Then I tried:

RetVal = Shell("C:\Windows\System32\cmd.exe " & "python " & "C:\\Python27\\hello.py")

It starts command prompt but does not even start python.

P.S. I checked all the related questions in the forum, they do not solve my prob.

解决方案

Try this:

RetVal = Shell("<full path to python.exe> " & "<full path to your python script>")

Or if the python script is in the same folder as the workbook, then you can try :

RetVal = Shell("<full path to python.exe> " & ActiveWorkBook.Path & "\<python script name>")

All details within <> are to be given. <> - indicates changeable fields

I guess this should work. But then again, if your script is going to call other files which are in different folders, it can cause errors unless your script has properly handled it. Hope it helps.

这篇关于如何在excel vba上调用python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆