I have an issue when I try to call a python script from excel, with a vba macro. I can execute the script just one time but if I try to execute it several times I have to close excel book a re-open it every time I want to execute the script. I call the macro from a button.
I have the xlwings addin for excel installed.
The Python script reads an Excel sheet table, in a Pandas DataFrame, performed some calculations, pasted the calculation column in the first blank column of the Excel table and finally saved the workbook.
The first time I push the button in excel to execute the script it works properly, but if I want to run the script another time when I push the button happens nothing.
I tried searching google but I didn't find anything similar to this issue.
The vba macro code is:
sub MacroName()
RunPhyton("import script_name")
end sub()
And the python code is:
import pandas as pd
import xlwings as xw
def myfunc(arg1, arg2, arg3):
pass
Book = xw.Book.caller()
df = Book.sheets('sht_name').range('rng_name').options(pd.DataFrame,
expand='table',
index=False).value
df['col_name'] = df.apply(lambda x: myfunc(x['arg1'],
x['arg2'],
x['arg3']), axis=1)
Book.sheets('sht_name').range('rng_name').options(index = False).value =\
df['col_name']
Book.save()
The python code works properly.
I expected that the python script runs every time I press the button to run the vba macro without having to close the excel book and open it every time I want to run the script.
The second and subsequent times I press the button to run the macro nothing happens, I also don't get any error message.