Python Forum
How do I reduce the time to Invoke Macro via Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I reduce the time to Invoke Macro via Python?
#1
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
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')))
Reply
#2
you never call open_run_macro_file.
what do you think - how much of the "delay" is related to time.sleep(6)?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,759 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Adding values with reduce() function from the list of tuples kinimod 10 2,513 Jan-24-2023, 08:22 AM
Last Post: perfringo
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 851 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,527 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,573 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,243 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  How to invoke a function with return statement in list comprehension? maiya 4 2,751 Jul-17-2021, 04:30 PM
Last Post: maiya
  Auto-populate Macro variables Spartan314 3 2,611 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  How to reduce the following code to run in sequence? Giggel 4 2,626 Jun-28-2020, 01:31 AM
Last Post: Giggel
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,538 Jun-14-2020, 05:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020