是否可以使用 openpyxl 运行 来自 excel 的 python 脚本?

Is it possible to run a python script from excel using openpyxl?

我正在查看 Python 的 openpyxl 模块文档,但我没有找到任何关于如何使用此模块从 excel 启动 Python 脚本的信息。这可能吗?

如前所述 in the official docsopenpyxl 库专为读取、编辑和创建 Excel 2010 文件而设计。因此,这个模块本身无法(在撰写本文时)启用 Excel 到 运行 Python 代码。

回答你的问题:没有。

我只是想这样做并遇到了这个线程。我知道它很旧,但有 499 次观看。我最后这样做了:

VBA:

Private Sub cmdRunPyScales_Click()
Dim strCommand As String
Dim lngErrorCode As Long
Dim wsh As WshShell
' From the VBA window, click Tools > References. In the pop-up window, check the box next to Windows Script Host Object Model:
    
    Set wsh = New WshShell
    strCommand = Chr(34) & _
                 "C:\Users\cliff\Documents Units\PyScales.bat" & _
                 Chr(34)
    Debug.Print strCommand
    lngErrorCode = wsh.Run(strCommand, _
                           WindowStyle:=1, _
                           WaitOnReturn:=True)
    If lngErrorCode <> 0 Then
        MsgBox "Uh oh! Something went wrong with the batch file!" & lngErrorCode
    End If
    
End Sub

在PyScales.bat中:

echo off
echo %time%
cd\
cd c:\users\cliff\PycharmProjects\ExcelProject
python PyScales.py
echo %time%
pause

对我有用。