gpt4 book ai didi

python - 从 MS Office 运行 Python 脚本

转载 作者:行者123 更新时间:2023-11-28 16:53:39 24 4
gpt4 key购买 nike

我已经安装了 PythonWin..我可以从 Python 读取和写入 Excel,这不是问题。不是我需要的用法。我发现的所有示例都比我需要的更复杂。因为,我要搬走从 Excel,我需要半步进行测试。

从 Excel 启动 python 脚本的最简单方法是什么。我不需要图形用户界面。用法:在打开 xls 时执行 python 脚本。没有什么花哨。

现在,我只是在打开 xls 之前手动执行脚本。

Private Sub Workbook_Open()

MyPythonScript.pyw ' this is where scripts should go. just one is all I need.

End Sub

最佳答案

您可以使用 Excel 的 Shell 函数*,例如

Sub RunExternalProg()

Dim return_value As Double
return_value = Shell("C:\Python26\pythonw.exe C:\my_script.py", vbHide)
Debug.Print return_value

End Sub

您可能需要更改pythonw 可执行文件的路径;取决于您的设置。

*Shell runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.

关于python - 从 MS Office 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3815340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com