-2

I am reviewing the documentation of openpyxl modeule for Python, but I do not find any information regarding how to launch a Python script from excel using this module. Is that possible?

Pegaso
  • 115
  • 2
  • 13
  • 1
    So you want Excel to launch a script that uses a library that allows for access to the contents of Excel spreadsheets, when Excel is (arguably) better at doing that? – Scott Hunter Jan 25 '18 at 22:27
  • Thanks Scott. I need to find a solution that will allow me to run a python script from excel (script that will read data form the aforementioned excel, therefore, I think I should pass a function, rather than the script). My question is: Is openpyxl is the right tool for the job? – Pegaso Jan 25 '18 at 22:40
  • Have you seen SO question [Run python script from VBA](https://stackoverflow.com/questions/37947724/run-python-script-from-vba) – zelusp Jan 25 '18 at 22:44
  • Thanks zelusp. I tried xlwings weeks ago but I wasn't able to make it run, that's why I decided to find alternatives, and basically I just want to know if someone can indicate if openpyxl would work, and if that's not the case, what would do. I also checked the freezing option, but that would not be very convenient since I have different scripts to run, so I would like to give the user the flexibility to run the version of the scrypt they will need (there will be around 10 different versions to choose by the user) – Pegaso Jan 25 '18 at 22:49
  • perhaps [ExcelPython - Write a user-defined function in Python](http://ericremoreynolds.github.io/excelpython/quickstart/), or [Python in Excel with PyXLL](https://www.pyxll.com/) are helpful – chickity china chinese chicken Jan 25 '18 at 23:15

2 Answers2

1

As mentioned in the official docs, the openpyxl library is designed specifically to read, edit and create Excel 2010 files. As such, this module by itself is incapable (at the time of writing) of enabling Excel to run Python code.

To answer your question: no.

trk
  • 342
  • 4
  • 13
0

I'm just trying to do this and came across this thread. I know it's old but there's 499 views. I did this in the end:

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\01 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

In PyScales.bat:

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

It works for me.

David Buck
  • 3,752
  • 35
  • 31
  • 35