3

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

After testing could I run Shell(.....) as one line Workook_open.... Or does it to return_value
Sure; make it a one-liner. There is no need to print the return value if you don't have a use for it. It may be good to check for a return-value of zero; however, thereby allowing your code to gracefully handle that possibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.