我从 VBA 调用 Python 脚本。此 Python 脚本的结构是一个函数,没有任何其他内容:
def myFunction():
# do stuff
return stuff_output
我知道这个函数可以正常工作,因为如果添加,print(myFunction())
在命令行中生成正确的输出(JSON 响应)。
在我的 VBA 代码中,“RetVal”始终是 4 位整数值。也许Python函数甚至没有被执行?
我应该修复哪些代码以及如何修复才能使其正常工作?如果可能的话,我希望有一个接近我当前代码且无需外部库的解决方案。
Option Explicit
Sub RunPython()
Dim RetVal
Dim scriptPath As String
scriptPath = "C:\Users\Rychu\Desktop\python\myPythonScript.py"
RetVal = Shell("C:\Python36-32\python.exe" & " " & scriptPath, vbHide)
If RetVal = 0 Then
MsgBox "Couldn't run python script!", vbOKOnly
Else
Debug.Print RetVal
End If
End Sub
最佳答案
我找不到任何直接的方法来传输Python函数返回的值,因此我通过将Python结果保存到桌面上的文本文件并使用VBA读取+删除同一文件来处理它。
重要的事情是:
shellObj.Run
与Shell
不同,允许设置一个 bool 参数来等待返回True
,以便其余的当尚未创建任何文本文件时,VBA 代码不会尝试从文本文件中获取数据。如果文本文件没有指定完整的文件路径,只有文件名,Python 不会在 Python 脚本所在的目录中创建文本文件,就像我通过命令行运行脚本时发生的情况一样或 Visual Studio 。显然,当从 VBA 调用时,Python 对默认目录有不同的看法,它也不是 VBA 文件的目录。
这是我的 Python 代码:
def myFunction():
# do stuff
return stuff_output
import os
f = open(os.path.expanduser("~\Desktop") + "\myTemp.txt","w") #create a new text file on Desktop in write mode
f.write(str(myFunction())) #write output to created text file
f.close() #close file object
这是我的 VBA 代码:
Option Explicit
Sub RunPython()
Dim shellObj As Object: Set shellObj = CreateObject("WScript.Shell")
Dim tempTxtPath As String
Const scriptPath As String = "C:\Users\Rychu\Desktop\python\myPythonScript.py"
Const pythonPath As String = "C:\Python36-32\python.exe"
tempTxtPath = CreateObject("WScript.Shell").specialfolders("Desktop") & "\myTemp.txt"
shellObj.Run pythonPath & " " & scriptPath, vbHide, True
Debug.Print (getTextAndDelete(tempTxtPath))
End Sub
Function getTextAndDelete(filePath) As String
Dim fileNum As Long
Dim myData As String
fileNum = FreeFile
Open filePath For Binary As #fileNum
myData = Space$(LOF(1))
Get #fileNum, , myData
Close #fileNum
Kill filePath
getTextAndDelete = myData
End Function
关于python - 从 VBA 调用 Python - 返回 Python 函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915099/