I have created a Python script to change my background image based on the time of the day (Windows 10 user). If the current time it's past the sunset time, then a specific image will be shown, if it's past the sunrise time, then there would be another one.
The sunrise/sunset data is being taken from a published source from an Excel spreadsheet.
What I want is to have this Python code running in the background instead of creating a Task Scheduler job to be ran every 30 seconds. Is there a better way to approach the code below in order to realize this?
from datetime import datetime
import pandas
import ctypes
file_path = "myfile.xlsx"
data = pandas.read_excel(file_path, header=0) #Column lines on line 0
#Today as day number
day = datetime.now().timetuple().tm_yday
#Today's parameters
sunrise = data["sr"][day-1] #sr is a column name in the Excel spreadsheet; Minus 1 to account for 0 based indexing;
sunset = data["ss"][day-1] #ss is a column name in the Excel spreadsheet; Minus 1 to account for 0 based indexing;
#Time right now
now = datetime.now().time()
#Setting up the day_night variable depending on the now variable
if now > sunrise and now < sunset:
day_night = 'day'
else:
day_night = 'night'
#The path to the wallpapers being used
path = 'C:\\wallpapers\\'+ day_night +'.jpg'
SPI_SETDESKWALLPAPER = 20
#Function to change the wallpaper
def changeBG(path):
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path, 3)
changeBG(path)
Sorry in advance for the messy code. Yesterday was my first day writing code for this kind of purposes.
Task Scheduler, which is meant to run something periodically in the background on Windows ?