此宏可以在Mac上的Excel 2011上正常运行:
Sub Works()
scriptToRun = "do shell script ""/anaconda/bin/python -c "" & quoted form of ""import test_file"" "
res = MacScript(scriptToRun)
Debug.Print res
End Sub
相应的test_file.py
文件(将其放置在pythonpath上的某个位置)是:
import sys
print(sys.version)
但是,当我涉及appscript
Python程序包时,它冻结了很长时间,并且什么也不做。将test_file.py
文件更改为此(pip install appscript
第一个):
import appscript
app = appscript.app('Microsoft Excel')
app.cells['A1'].value.set(2)
但是,当我直接从AppleScript编辑器运行命令时,此方法有效:
do shell script "/anaconda/bin/python -c " & quoted form of "import test_file"
如何在Mac上从Excel VBA调用此方法?(通过Windows的方式,它可以轻松地与WScript.Shell
和配合使用pywin32
)
解决方案是使用system()/popen()
来自此答案的调用,并在命令末尾以&符的形式运行该命令作为后台进程:
Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long
Sub RunTest()
Dim result As String
result = system("/anaconda/bin/python -c 'import test_file' &")
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句