从excel vba运行python脚本使用Shell什么也不做 [英] Running python script from excel vba using Shell does nothing

查看:627
本文介绍了从excel vba运行python脚本使用Shell什么也不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Shell(python& ActiveWorkbook),我正在尝试在excel VBA中运行一个python脚本。 Path&\CreateVolsurface.py -d10 Sep 12)

它什么都不做如果我看任务管理器,甚至没有创建一个python进程。如果我调试并复制在上面的代码中创建的字符串,并将其粘贴到命令提示符下,它将完美运行。



python脚本将日期(作为字符串)作为参数创建一个图像文件。



任何想法为什么这不工作,或者我可以如何调试?



我也试过这样:

  Shell(python CreateVolsurface.py -d10 Sep 12)

,这样:

  ret_val = Shell(python& ActiveWorkbook.Path& \\ CreateVolsurface.py -d10 Sep 12,vbHide)

但他们也做






问题是vba中的 shell()在其默认文件夹中启动cmd.exe,而不是当前工作簿的活动文件夹。在我的python脚本中,我有相对路径引用是一个问题。为了解决这个问题,我使用了我的python脚本中的os模块来更改当前的目录。

解决方案

尝试指定路径到python.exe,或者您可能需要添加到%PATH%环境变量的路径。



尝试以下操作来验证Shell正在使用调试器:

  ret_val = Shell(notepad.exe,vbNormalFocus)
pre>

如果有效,那么您应该尝试指定路径:

  args = ActiveWorkbook.Path& \CreateVolsurface.py -d10 Sep 12
ret_val = Shell(C:\python27\python.exe& args)

如果您需要检查python是否位于%PATH%环境变量中,则sysinternals的Process Explorer(procexp.exe)实用程序可以显示所有的运行过程的环境设置。


I am trying to run a python script in excel VBA like this:

Shell("python " & ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12""")

but it does nothing. If I watch the task manager, a python process doesn't even get created. If I debug and copy the string created in the code above and paste it into command prompt it runs perfectly.

The python script takes a date (as a string) as a parameter and creates an image file.

Any idea why this would not work or how I could debug it?

I've also tried this:

Shell ("python CreateVolsurface.py -d ""10 Sep 12""")

and this:

ret_val = Shell("python " & ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12""", vbHide)

but they also do nothing.


The issue was that shell() in vba starts cmd.exe in it's default folder and not in the active folder of the current workbook. In my python script I had relative path references which was an issue. To solve this I used the os module in my python script to change the current directory at the start.

解决方案

Try specifying the path to python.exe, or you may need to add the path to your %PATH% environment variable.

Try the following to verify Shell is working and step through with the debugger:

ret_val = Shell("notepad.exe", vbNormalFocus)

If that works, then you should try specifying the path:

args = ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12"""
ret_val = Shell("C:\python27\python.exe " & args)

If you need to check if python is in your %PATH% environment variable, the Process Explorer (procexp.exe) utility from sysinternals can show you all of the environment settings for a running process.

这篇关于从excel vba运行python脚本使用Shell什么也不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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