当我运行以下内容并将一些参数从Excel for Mac 2011 传递给我现有的python脚本时,它可以正常工作,但Excel for Mac 2016 没有任何反应,即没有错误没有结果。
#If Mac Then
Private Declare Function system Lib "libc.dylib" (ByVal command As String) As Long
Const python3Dir As String = "/usr/local/bin/python3"
#End If
Sub btnScrape_Click()
' Uses system() to run external command
' system() only returns the exit code. If you want to get the output from the command,
Dim result As String
Dim command As String
Dim myDir As String
myDir = getPosixPath(ThisWorkbook.Path)
command = "cd " & myDir & " && " & python3Dir & " myPythonScript.py ######## ####"
Debug.Print command
result = system(command)
Debug.Print result
' 0 means OK.
MsgBox "Check report.txt"
End Sub
Function getPosixPath(macPath As String) As String
Dim scriptToRun As String
scriptToRun = "tell application ""Finder""" & Chr(13)
scriptToRun = scriptToRun & "set appUserPath to POSIX path of " & """" & macPath & """" & Chr(13)
scriptToRun = scriptToRun & "end tell" & Chr(13)
getPosixPath = MacScript(scriptToRun)
End Function
Python设置:
which -a python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
/usr/local/bin/python3
which -a python
/usr/bin/python
echo $PATH
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
Excel for Mac 2016 是否需要修改/添加特定内容?
答案 0 :(得分:0)
我本人正在寻找有关如何在MacOS上获取Excel VBA以运行不是VBA的任何东西的答案。基本上,如何创建一个可以从Excel中“越狱”的函数。
但是,我能走的很远:MacScript是一种解决方案,可让您在Office 2011中使用薄的AppleScript包装器运行任何内容。在Office 2016中,由于“沙箱”,MacScript已被弃用。相反,我们应该使用AppleScriptTask指令。
通常的想法是,您必须在~/Library/Application Scripts/com.microsoft.Excel/
中创建一个AppleScript脚本,该脚本将以某种方式启动具有正确参数的python程序。除了一些有用的hints by Ron de Bruin之外,我找不到关于如何执行此操作的任何指示。到目前为止,最新的技术水平似乎是:“只要看看那边,祝你好运”。
因此,我们对VBA和Python的学习还不够,现在我们还应该学习AppleScript和MacOS的复杂性。我不确定Microsoft的人们在想什么,他们希望我们经历在一个晦涩的目录中安装AppleScript文件的所有循环,只是为了拥有运行python程序的特权。是否只是他们不希望我们这样做(“设计有缺陷”)?还是Microsoft-Apple贸易战的晦涩副作用?还是微软的“企业大脑”根本不知道存在问题?无论是什么情况,他们都没有告诉我们。
如果您只需要从应用程序运行Python脚本,而该脚本不会返回任何内容,则您可能有XLWings的解决方案,该解决方案提供了指令RunPython。稍有耐心,您就可以为它提供可变的参数。不幸的是,他们对用Python编写的Excel函数的出色想法只能在Windows上运行,而不能在MacOS上运行(难怪)。
但是,如果像我一样,如果您确实想使用VBA从Python脚本中获取价值,那么不幸的是,这仍然是一个不断陷入困境的探索。