如何从Excel Vba Mac直接执行Python?

问题描述 投票:0回答:1

首先我解决了在Conda环境下使用AppleScript执行Python的问题:如何在Apple系统中使用AppleScript执行Python?

现在我必须从我的 vba 代码运行 applescript,但我有错误或无操作。

文件和脚本与之前的链接相同,vba 代码:

第一次测试

Sub RunScript0()
    Dim myScriptResult As String
    myScriptResult = AppleScriptTask("/Users/andrea/Library/Application Scripts/com.microsoft.Excel/MyAppleScriptFile.scpt", "MyAppleScriptHandler", "/usr/bin/python3 /Users/andrea/Desktop/pymac/test.py")
End Sub

第二次测试

Sub RunScript1()
    Dim scriptPath As String
    Dim command As String

    ' Set the path to your AppleScript file
    scriptPath = "/Users/andrea/Library/Application Scripts/com.microsoft.Excel/run_script.scpt"

    ' Build the full command
    command = "osascript """ & scriptPath & """"

    ' Run the command using the Shell function
    Call Shell(command, vbNormalFocus)
End Sub

代码错误,无操作。

python excel vba applescript
1个回答
0
投票

适用于苹果电脑的 Excel 365 osX 索诺玛 14.2

我简单地解决了:

  1. 将 applescript 文件的别名(位于 /Users/andrea/Library/Application Scripts/com.microsoft.Excel)放入工作 Excel 文件夹文件(位于 /Users/andrea/Desktop/pymac)中
  2. 我的苹果脚本文件:

on myapplescripthandler(paramString)

do shell script "/usr/bin/python3 " & paramString

end myapplescripthandler

  1. 我的调用applescript的vba代码:

    子运行脚本()

    Dim res As String
    Dim myScriptResult As String
    
    myScriptResult = AppleScriptTask("PythonTest.scpt", _ 
         "myapplescripthandler", _ 
         "/Users/andrea/Desktop/pymac/test.py")
    
    MsgBox myScriptResult
    End Sub
    

就这些了,希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.