I have a problem with my VBA code to call a Python Script, which downloads a CSV from a web page. In Visual Studio Code, I can execute it and the date of my result file changes. But, if I try to call this script in VBA, open python.exe
, do something (I don't know), and close it, and my date of my csv file doesn't upload.
My VBA code is:
Sub Download_CSV_Python()
'Procedure to run a Python Script in a VBA Procedure using the shell
'Declaration
Dim objShell As Object 'For the Shell
Dim Pythonexe, PythonScript As String
'Create the Shell Object
Set objShell = VBA.CreateObject("WScript.Shell")
'path of the python.exe
Pythonexe = """C:\Users\sergi\AppData\Local\Programs\Python\Python310\python.exe"""
'path of your Python script
PythonScript = "C:\Users\sergi\Desktop\Python\CSVChrome4.py"
'Run your Python script
objShell.Run Pythonexe & PythonScript
'free variables for memory
Set objShell = Nothing
End Sub
from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-GB', tz=360)
colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")
dataset = []
for x in range(0,len(df2)):
keywords = [df2[x]]
pytrend.build_payload(
kw_list=keywords,
cat=0,
timeframe='today 12-m',
geo='')
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
dataset.append(data)
result = pd.concat(dataset, axis=1)
result.to_csv('search_trends.csv')
executionTime = (time.time() - startTime)
print('Execution time in sec.: ' + str(executionTime))
enter code here