0

我正在使用具有基于 VBA / Winwrap Basic 的编程接口的软件。我想使用 Python 中的一些机器学习库,但我不确定如何有效地构建它。

该软件是一个动态仿真程序。每隔几个时间步,我想使用 Pyhton 库用新数据更新(训练)人工神经网络(ANN)。我可以看到可以调用脚本(例如如何在 Excel vba 上调用 python 脚本?)。因为我必须每隔几次调用一次脚本,所以我不确定如何处理 ANN 对象。实现这一点的最佳方法是什么?

4

1 回答 1

1

如果我理解正确,您想调用 Python 脚本并传递一个参数,然后可能处理返回的数据对象?如果您使用 Windows 并拥有 VBA 脚本环境,那么也许您可以使用 WSH(Windows 脚本宿主)对象来运行脚本。首先声明对象

Dim wsh As New IWshRuntimeLibrary.WshShell  'early binding

Dim wsh As Object
Set wsh = CreateObject("Wscript.Shell") ' late binding

并在对象上使用 Run() 方法,将对象和参数作为字符串传递。在此示例中,我调用 powershell 并将脚本文件(run.ps1)作为参数传递。我只是调用方法,但没有从脚本中获取返回对象。

res = wsh.Run("powershell path\run.ps1", WindowStyle:=False, WaitOnReturn:=True)
If res = 0 Then
    Call MsgBox("script ran successfully")
Else
    Call MsgBox("script ran unsuccessfully")
End If

另一方面,在此示例中,我使用 Exec 方法将返回数据获取为 json

res = wsh.Exec("powershell path\run.ps1").StdOut.ReadAll()

Debug.Print res ' return data:

returnObject: 
{
    title: 'theTitle', 
    value: 'the value',
    status: 'status'
}

run.ps1 基本上返回一个字符串:

return "returnObject: 
{
    title: 'theTitle', 
    value: 'the value',
    status: 'status'
}"

编辑:调用 python 脚本文件使用pythonpy就地为 powershell

res = wsh.Exec("python path\pythonfile.py").StdOut.ReadAll()
于 2018-11-13T01:48:21.853 回答