Pass Python Argument Through Vba
I have written a python script that needs to be called using VBA. I have written a python script that can parse through a pdf, store the data I need in variables, and write to a pr
Solution 1:
Make sure you import sys and read the appropriate arg within the script then in VBA call with space then the argument e.g.
test.py:
import sys
withopen(f'C:/Users/User/OneDrive/Desktop/{sys.argv[1]}', 'w') as f:
f.write(b'some text')
VBA:
Sub RunPythonScript()
'Declare VariablesDim objShell AsObjectDim PythonExe, PythonScript AsString'Create a new Object shell.Set objShell = VBA.CreateObject("Wscript.Shell")
'Provide file path to Python.exe'USE TRIPLE QUOTES WHEN FILE PATH CONTAINS SPACES.
PythonExe = """C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe"""
PythonScript = "C:\Users\User\OneDrive\Desktop\test.py"
'Run the Python Script
objShell.Run PythonExe & Chr$(32) & PythonScript & Chr$(32) & "Argument.txt"
End Sub
Post a Comment for "Pass Python Argument Through Vba"