1

I want to create a Python script to have my wallpaper changed based on some specific times (sunset and sunrise to be more precise), with 2 pictures alternating between night and day.

For the wallpaper change part I've managed to put something together which kind of works, but now I've hit an wall and I don't know which way to go.

I have an Excel spreadsheet with 3 columns (day_of_year -containing dates-, sunrise -containing time-, sunset -containing time-), and I want to have 2 variables created in Python (sunrise_today and sunset_today) which will bring me from the spreadsheet the corresponding values for today. In Excel environment this would be called a VLOOKUP using TODAY() as key.

This is pretty much what I want:

screenshot

instaspam
  • 32
  • 2
  • 8
  • Have you tried using Pandas to imput the spreadsheet? Pandas has all kinds of utilities for handling date/time data. – Donna Apr 24 '21 at 15:58
  • A little off-topic: You can compute the sun rise and set times in Python and forget about the spreadsheet. See my answer to the question [Javascript or Python - How do I figure out if it's night or day?](https://stackoverflow.com/questions/15044521/javascript-or-python-how-do-i-figure-out-if-its-night-or-day) – martineau Apr 24 '21 at 17:49
  • @martineau Thank you for the input. The thing is I would want to use some official/published data for civil sunrise/sunset based on a specific location. I've tried several options so far that can answer my questions, but sadly haven't found anything (there is always a difference of 1 to 10 minutes between the published data and the output). – instaspam Apr 24 '21 at 17:57
  • The code in my answer will work for any location given its longitude and latitude (plus timezone). However I understand needing to read the spreadsheet if you are wanting to use some sort of published data. Note there are a number of third-party modules available that provide ways of reading (and writing) xlsx format files). – martineau Apr 24 '21 at 18:08

2 Answers2

0

It's probably not the best way, but you can do it using pandas and openpyxl:

import pandas as pd
from openpyxl import load_workbook

xls_file = "YourFile.xls"
xls_sheet = "YourSheet"
XLSXreader=pd.ExcelFile(xls_file, engine = 'openpyxl')
INP_Data = pd.read_excel(XLSXreader,sheet_name=xls_sheet,eader=1)

This will generate a INP_Data, which is a Pandas dataframe. You cn access the values by:

INP_Data['YourHeaderName']

You can convert it to numpy array by:

INP_Data['YourHeaderName'].to_numpy()

Check which line is your current date:

Date_Line = INP_Data['YourHeaderName'].to_numpy() == YourCurrentDate

And then get the Rising Time by

RisingTime = INP_Data['RisinTimeHeader'].to_numpy()[Date_Line]

Note: take a look at the pd.read_excel function in order to define the header lines and columns you want to read.

Hope to help someway. Cheers!!

  • Thanks a lot! This would be a great starting point. I will test this out and come back with a feedback. – instaspam Apr 24 '21 at 16:06
0

This should pull the proper row with minimal fuss.

from datetime import datetime
import pandas

file_path = "Your_Excel_File.xls"
data = pandas.read_excel(file_path, header=0) #I assume the column names are on line 0

#Find today as day of year
day = datetime.now().timetuple().tm_yday

#Grab today's info
sunrise = data["sunrise"][day-1] #Minus 1 to account for 0 based indexing
sunset = data["sunset"][day-1]
  • Thank you for your reply! Everything is clear and concise. However, I tried your approach and I got the following Syntax error. For some reason I think it's something I'm missing regarding the way I should be approaching the file_path. **File "", line 4 file_path = "C:\Users\mariu\Desktop\DateTime.xlsx" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape** – instaspam Apr 24 '21 at 17:28
  • @instaspam: FYI, you can use a limited version of markdown to format your questions, comment, and answers quite nicely. [Here's some formatting help](https://stackoverflow.com/help/formatting). – martineau Apr 24 '21 at 17:46
  • Coming back to finally close the topic. Apparently there was a typo in my code where I called the column headers "Sunrise" and "Sunset", with capital letters, and in the function they were called "sunset" and "sunrise" (lowercase). I re-checked my code, changed the header names to better fit the code, all good now. Thanks a lot! – instaspam Apr 24 '21 at 18:53