0

I’m relatively new to Python and I’m trying to figure out how to set up a timer in my script for it to run daily at a specific time instead of using Windows Task Scheduler.

The below script opens a macro enabled excel spreadsheet and runs the macro. I’ve verified that everything is working as expected.

#REFERENCE FILE PATH AND RUN MACROS#
import os
import win32com
import win32com.client

# path where macro enable sheet is
if os.path.exists ("C:\\Users\\hernri01\\Documents\\Python Scripts\\SDK Cert 
Source\\Macro Autosave\\Macro_SDKtoXLSX.xlsm"):
    xl = win32com.client.Dispatch('Excel.Application')
    xl.Workbooks.Open(Filename = "C:\\Users\\hernri01\\Documents\\Python 
    Scripts\\SDK Cert Source\\Macro Autosave\\Macro_SDKtoXLSX.xlsm", 
    ReadOnly=1)

    # runs macro
    xl.Application.Run("openfilesandsave")
    del xl

#PRINT FINAL COMPLETED MESSAGE
print("Macro refresh completed!")

When I try to add the Timer I am receiving an error message. Script and error below:

#REFERENCE FILE PATH AND RUN MACROS#
import os
import win32com
import win32com.client
import datetime
from threading import Timer

xl = win32com.client.Dispatch('Excel.Application')
def run_prog():
    xl.Workbooks.Open(Filename = "C:\\Users\\hernri01\\Documents\\Python 
    Scripts\\SDK Cert Source\\Macro Autosave\\Macro_SDKtoXLSX.xlsm", 
    ReadOnly=1)
    xl.Application.Run("openfilesandsave")
    xl.Workbooks.Close()

    #PRINT FINAL COMPLETED MESSAGE#
    print("Macro refresh completed!")

    # how often python to run
    #t = Timer(3600 * 24,run_prog) #3600 = seconds in 1 hour
    t = Timer(10,run_prog) # 10 = run every 10 seconds
    t.start()

x=datetime.datetime.today()
#y=x.replace(day=x.day, hour=4, minute=0, second=0, microsecond=0)
y=x.replace(day=x.day, hour=11, minute=56, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1
print (secs)

t = Timer(secs, run_prog)
t.start()

print("Macro refresh completed!")

Error Message:

48
Macro refresh completed!
>>> Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\hernri01\AppData\Local\Programs\Python\Python37- 
32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\hernri01\AppData\Local\Programs\Python\Python37- 
32\lib\threading.py", line 1158, in run
self.function(*self.args, **self.kwargs)
File "C:\Users\hernri01\Documents\Python Scripts\SDK Cert Source\Macro 
Autosave\openFileSave.py", line 14, in run_prog

Thanks for any help on this.

BonusCup
  • 13
  • 5
  • 1
    why don't you just sleep for 86400 seconds? – Liam Aug 13 '18 at 16:16
  • Thanks, I did not know about that function. Researching it now. – BonusCup Aug 13 '18 at 16:28
  • Trying to understand why task scheduler is not an option ? As to do the above you will have to have the python.exe permanently running no ? – RK1 Aug 13 '18 at 20:41
  • @RK1, for some reason you need admin privileges at my workplace for Task Scheduler. – BonusCup Aug 14 '18 at 00:46
  • Ah I see, could you try set-up the task directly in the cmd https://stackoverflow.com/questions/19641619/windows-7-scheduled-task-command-line not sure if this is feasible but might be helpful too see point 2: https://kuzzmi.com/blog/be-productive-without-administrator-rights/ – RK1 Aug 14 '18 at 07:33

0 Answers0