【问题标题】:Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac 2011)来自 Excel VBA 的 Shell 调用冻结 - 涉及 MacScript 和 Python(Excel for Mac 2011)
【发布时间】:2014-07-28 15:42:39
【问题描述】:

此宏在 Excel 2011 for Mac 上运行良好:

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.Shellpywin32 一起使用)

【问题讨论】:

    标签: python macos vba excel


    【解决方案1】:

    解决方案是使用来自此answersystem()/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
    

    【讨论】:

      猜你喜欢
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多