Home » excel » excel – Running Python Scripts From MS Office

excel – Running Python Scripts From MS Office

Posted by: admin April 23, 2020 Leave a comment


I have installed PythonWin installed..
I can read and write to Excel from Python, not a problem. Not the usage I need.
All examples I have found are more complex than I need. Since, I’m moving away
from Excel, I need a half steps for testing.

Whats the simplest way to fire off python scripts from Excel. I dont need gui.
Usage: On open of xls excute python script. Nothing fancy.

Right now, I simply execute the scripts manually before opening xls.

Private Sub Workbook_Open()

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

End Sub
How to&Answers:

You can use Excel’s Shell function*, e.g.

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

You may need to change the path to the pythonw executable; depending on your setup.

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