user20004947
user20004947

Reputation:

Run a python script with a button

I am trying to create a button that will run a Python script.

I tried with a ipynb not a py file.

I tried the following VBA code:

Sub RunPythonScript()

Dim objShell As Object
Dim PythonExePath As String, PythonScriptPath As String
ActiveWorkbook.Save

Set objShell = VBA.CreateObject("Wscript.Shell")

PythonExePath = """C:\Users\example"""
PythonScriptPath = """Desktop\1\example.ipynb"""

objShell.Run PythonExePath & PythonScriptPath

End Sub

It looks like it's working but it doesn't do anything. It might be I used the wrong code or because the Python code is too complex for this as it's connected to a postressql base and bringing data from there.

How should I automate the Python? Maybe Excel macro button or an executable file?

Upvotes: 0

Views: 264

Answers (1)

daveyvdweide
daveyvdweide

Reputation: 117

Your PythonExePath variable is not set correctly. It does not point towards your python installation. Locate your python installation, and don't forget to include the executable file in the PythonExePath variable.

It currenctly is:

PythonExePath = """C:\Users\example"""

And should be something like:

PythonExePath = """C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe"""

Link to duplicate question

Upvotes: 1

Related Questions