来自Excel VBA的Shell调用冻结 - 涉及到MacScript和Python(Excel for Mac 2011) [英] Shell call from Excel VBA freezes - involves MacScript and Python (Excel for Mac 2011)

查看:1078
本文介绍了来自Excel VBA的Shell调用冻结 - 涉及到MacScript和Python(Excel for Mac 2011)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Sub Works()
scriptToRun =do shell脚本/ anaconda / bin / python -c引用的形式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编辑器运行命令时,这样做有效:

 做shell脚本/ anaconda / bin / python -c&引用的表单import test_file

如何在Mac上从Excel VBA调用? (顺便说一句,在Windows上,它可以很容易地与 WScript.Shell pywin32

解决方案

解决方案是使用这个 system()/ popen() =https://stackoverflow.com/a/12320294/918626>回答,并在后台运行命令作为后台进程,最后使用&符号:

 私有声明函数系统Liblibc.dylib(ByVal命令As String)As Long 

Sub RunTest()
Dim result As String
result = system(/ anaconda / bin / python -cimport test_file'&)
End Sub


This macro runs fine on 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

The according test_file.py file (place it somewhere on the pythonpath) is:

import sys
print(sys.version)

However, when I involve the appscript Python package, it freezes for a very long time and does nothing. Change the test_file.py file to this (pip install appscript first):

import appscript
app = appscript.app('Microsoft Excel')
app.cells['A1'].value.set(2)

But when I run the command directly from the AppleScript Editor, this works:

do shell script "/anaconda/bin/python -c " & quoted form of "import test_file"

How can I call this from Excel VBA on Mac? (By the way on Windows, it works easily with WScript.Shell and pywin32)

解决方案

The solution was to use the system()/popen() call from this answer and run the command as background process with an ampersand at the end:

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

这篇关于来自Excel VBA的Shell调用冻结 - 涉及到MacScript和Python(Excel for Mac 2011)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆