首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从VBA调用Python脚本

从VBA调用Python脚本
EN

Stack Overflow用户
提问于 2018-06-12 04:26:53
回答 2查看 1.8K关注 0票数 1

我在Excel中从vba调用python脚本时遇到问题。我阅读了解决相同问题的其他线程,但当我运行代码时,Python屏幕闪烁,然后消失。仅供参考,我下载了适用于Windows 10的python 3.6.5并将其添加到PATH中。谁能告诉我为什么屏幕会闪烁,我能做些什么来解决这个问题?谢谢

代码语言:javascript
复制
Sub Run_python()

Dim Ret_Val
Dim args As String

args = "C:\Users\opera\AppData\Local\Programs\Python\Python36\Tools\scripts\db2pickle.py"
Ret_Val = Shell("C:\Users\opera\AppData\Local\Programs\Python\Python36\python.exe" & " " & args, vbNormalFocus)

If Ret_Val = 0 Then
MsgBox "Couldn't run python script!", vbOKOnly
End If

End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-12 05:03:38

屏幕闪烁并快速返回,因为程序要么失败,要么运行到完成的速度如此之快

您是否尝试过从命令行运行它来查看发生了什么?

如果运行正常,您可以强制命令窗口保持打开状态,这样就可以从Shell命令中读取使用python时发生的情况

代码语言:javascript
复制
Call Shell("cmd.exe /S /K" & "C:\Users\opera\AppData\Local\Programs\Python\Python36\python.exe" & " " & args, vbNormalFocus)

/S      Modifies the treatment of string after /C or /K 
/C      Carries out the command specified by string and then terminates 
/K      Carries out the command specified by string but remains 
票数 0
EN

Stack Overflow用户

发布于 2020-04-07 11:25:22

我按照建议尝试了以下方法:

代码语言:javascript
复制
Dim RetVal
args = """C:\Users\something\Desktop\md\test.py"""
RetVal = Shell("C:\Python37\python.exe " & " " & args)
If RetVal = 0 Then
   MsgBox "Couldn't run python script!", vbOKOnly
End If

运行脚本后,命令窗口开始闪烁。所以,我只是添加了input(‘按任意键退出’)语句作为我的python脚本的最后一行,现在窗口仍然存在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50805511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档