Dec-28-2020, 03:47 AM
I noted that when I invoke a macro (via Python) to do some stuff in my Excel workbook it takes longer than if I write the code in Python to do it directly. The time difference is about 50% less.
Is there a way to reduce the time associated with invoking the macro? My macro code is rather long so it will be tedious if I were to re-code it in Python directly.
A simplified code
Is there a way to reduce the time associated with invoking the macro? My macro code is rather long so it will be tedious if I were to re-code it in Python directly.
A simplified code
import time import datetime import win32com.client as w3c macro_file='C:/Desktop/test_file1.xlsm' def open_run_macro_file(macro_name): xk=w3c.DispatchEx("Excel.Application") wb5=xk.Workbooks.Open(Filename=macro_file, ReadOnly=True, UpdateLinks=0, IgnoreReadOnlyRecommended=True) RPA_sht3=wb5.Worksheets('Sheet1') xk.Application.Run(macro_name) time.sleep(6) dyn_last_rw=int(RPA_sht3.cells(3, 2).value) return dyn_last_rw xk.Application.Quit() del xk del wb5 # time stamp to estimate start-time print(str(datetime.datetime.now().strftime('%H%M%S'))) # Code used to invoke the macro to do the task #open_run_macro_file('test_test') # Code used to get Python to do the task directly xl=w3c.DispatchEx("Excel.Application") wb3=xl.Workbooks.Open(Filename=macro_file, ReadOnly=False, UpdateLinks=0, IgnoreReadOnlyRecommended=True) sht3=wb3.Worksheets('Sheet1') sht3.cells(1, 1).value='test' wb3.Close(SaveChanges=False) xl.Application.Quit() del xl del wb3 # time stamp to estimate end-time print(str(datetime.datetime.now().strftime('%H%M%S')))