How can I launch an external python process from Excel 365 VBA on OSX?

It took me a while, but I figured out how to do it.

Excel 2016 has replaced MacScript() with AppleScriptTask(). This needs to be given an AppleScript file and function to call, and can't, as far as I am aware, be given raw AppleScript commands.

I created an AppleScript file called PythonCommand.scpt which contained something like:

on PythonCommand(pythonScript)

do shell script "/usr/local/bin/python " & pythonScript

end PythonCommand

This AppleScript file needs to be then placed in ~/Library/Application Scripts/com.microsoft.Excel/ (create this folder if it doesn't exist)

Once this is here, the function can be called using:

Dim result As String

result = AppleScriptTask("PythonCommand.scpt", "PythonCommand", pythonScript)

(it insists on returning something - it won't work without this)

A security proviso:

Any Excel macro can call this AppleScript. Macros can do nasty things anyway, but it would be worth putting some protection into it anyway to avoid additional code being executed.


翻译成人话:

mkdir -p ~/Library/Application\ Scripts/com.microsoft.Excel
vim PythonCommand.scpt

i输入以下内容

on PythonCommand(pythonScript)
do shell script "python -c '" & pythonScript & "'"
end PythonCommand

保存

然后在 Excel 新建一个 vba 模块

Sub CallPython()
s = AppleScriptTask("PythonCommand.scpt", "PythonCommand", "print(42)")
MsgBox (s)
End Sub

执行

have fun!

最新文章

  1. 使用crypto模块实现md5加密功能(解决中文加密前后端不一致的问题)
  2. HBA相关知识
  3. jquery 匿名函数的区别
  4. OSG使用更新回调来更改模型
  5. Unity 烘焙材质到单一贴图的脚本
  6. 虚拟化之esxi命令行管理
  7. delphi调用 java 的 WebService服务端.
  8. Linux cpuinfo 详解
  9. Mathematica 10 Mac 设置默认工作目录
  10. windows中vim-airline研究笔记
  11. jdk源码阅读笔记-ArrayList
  12. 405 Method Not Allowed error with PUT or DELETE Request on IIS Server
  13. Vue 核心之数据劫持
  14. ionic中android的返回键
  15. curl 命令模拟 HTTP GET/POST 请求
  16. tp5, laravel, yii2我该选择哪个
  17. ES6知识整理(10)--class的继承
  18. 雷林鹏分享: C# 教程
  19. tableViewCell上的定时器拖动阻塞
  20. unigui的ServerModule常用属性设置

热门文章

  1. 手把手教你玩转HarmonyOS版地图应用开发
  2. IE浏览器 查看Form对象
  3. Elasticsearch(9300、9200)未授权访问
  4. lanm环境
  5. Linux 基础指令初识
  6. JVM 内存分配、调优案例
  7. 控制流程之if判断与while、for循环
  8. 【LeetCode】860. 柠檬水找零
  9. 探讨UE4中的UBT和UHT
  10. SpringBoot开发十-开发登录,退出功能