Task Reminder system in Python!!

Task Reminder system in Python!!

If you are working on a computer for a lot of time daily then you might need a reminder which reminds you of your tasks which are to be done. In this article I will explain and show you how I created my own daily task reminder in python. This is a medium level python project so if you are not a pro in python you can still complete this project.

Intro to task reminder system.

In this task reminder system we will provide an excel sheet to the user where they will add their day schedule with time and task name to be reminded. We will use different python libraries like pandas, openpyxl, pyttsx3, plyer etc.

Our program will read the excel file submitted by the user and will check for the next task to be reminded. Our application will be in sleep mode until the time for next reminder comes. When there is the time to notify the user will be notified through voice as well as notification on windows. So lets begin coding our own custom task reminder. Start by creating an excel file.

Create an excel file(.xlsx) with three columns namely: time, am/pm and task and save this file in the same directory as your python file. In time column user can add the time at which they should be reminded same for the am/pm column. In the task column user will specify the task for which they should be reminded. Note that this column will be spoke by our program at reminding time.

Create Python File

Importing necessary libraries

import pandas as pd
from openpyxl import load_workbook
import time
from datetime import datetime
from plyer import notification
import pyttsx3

Pandas Dataframes will be used to loop over the workbook in order to figure out the schedule of user. Openpyxl is a module which is used to load excel workbooks. Time module is used to send program to sleep mode. Datetime module is used for date and time calculations. Plyer is a module used to send notification and pyttsx3 is used to generate voice output.

If you have not installed these libraries then open your terminal and write pip install commands to install these libraries. Or if you wish you can download the full source code with virtual environment which has all the necessary libraries installed in it from my GitHub Repository by clicking here .

Following are the commands to install required libraries. If you have already installed then ignore them.

pip install pandas
pip install plyer
pip install pyttsx3
pip install openpyxl

Initializing voice engine and speak function

engine = pyttsx3.init('sapi5')# Initialize engine for the voices
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)# Set voice for the reminder

def speak(audio):
    '''
    This function will be used to give output in form of voice.
    '''
    engine.say(audio)
    engine.runAndWait()

Here we are initializing the engine for voice output function. Sapi5 is a Microsoft speech API which will be used by our application to speak. In engine.setProperty('voice', voices[0].id) function you can specify voices[1].id for female voice. Here we have kept the male voice.

speak() function will take a string as its input and will convert the string to the voice output. this function will be used through the project to make our system speak.

Defining time cleaning and validating functions


def clean_time(t,m):
    '''
    This function will clean the time and convert it to 24 hours time.
    '''
    temp = int(t.split(':')[0])
    if m=='pm' and temp!=12:
        temp+=12
        return str(temp)+':'+str(t.split(':')[1])+':00'
    else:
        return t+':00'


def check_valid_time(tm):
    '''
    This function is used to check whether the entered time is valid or not.
    '''
    if int(tm.split(':')[0]) >=24:
        return False
    else:
        return True

In this section we will define two functions that will convert and validate the time inputted by the user. clean_timefunction() will take two arguments. First argument will be the time and second argument will be am/pm. It will convert the 12 hour inputted time in raw format to strict python datetime format. check_valid_time() will check whether the time inputted is valid or not.

Creating the main part


if __name__ == "__main__":
    diary = load_workbook('ReminderDiary.xlsx')['Sheet1'].values  # Loading workbook.
    next(diary) # Incrementing the excel sheet inorder to avoid header frame.
    df = pd.DataFrame(data=diary,columns=('time','am/pm','task','status'))# creating dataframe from excel.
    del diary   # deleting the workbook variable which is now of no longer use.

Here we are creating the main part of our system this will run when this specific file is run by the user. So here we are firstly loading the workbook 'Sheet1' in the diary variable. We are then incrementing the diary variable so it ignores the top heading row of our excel file using the next() function. Then on next line we are creating Pandas DataFrame from the data present in the workbook. Now let's delete the diary variable as it is of no use in order to save some memory.

Creating the reminder part


for index, row in df.iterrows():    # Iterating the diary, checking the schedule
        if row['time'] == None:
            break
        try:
            a_time = clean_time(row['time'].strftime('%H:%M:%S'),row['am/pm'])  # Converting to 24 hour time.
            if not(check_valid_time(a_time)):
                continue
            current_time = datetime.now().strftime('%H:%M:%S')  # getting current time.
            tdelta = datetime.strptime(a_time, '%H:%M:%S') - datetime.strptime(current_time, '%H:%M:%S')   # Calculating the time left for the next reminder.
            del current_time
            tdelta = str(tdelta).split(':')
            secs = int(int(tdelta[0])*3600+int(tdelta[1])*60+int(tdelta[2]))    # Converting the remaining time into seconds.
            del tdelta,a_time
            time.sleep(secs)    # Sending program to sleep mode until next event occurs.
            notification.notify(
                title=row['task'],
                app_icon="D:\Github Projects\Basic-Python-Projects\Task_Reminder\img\Task_Reminder.ico",
                message='Hey ayush its time to complete your following task... '+row['task'],
                timeout=3,
            )   # sending notification in windows.
            time.sleep(1)
            speak('Hey ayush its time to complete your task named '+row['task'])    # Notifying in a voice form.

        except Exception as e:
            print('There is some error in your scheduling')
            print(e)
    print('Its end of your schedule. Have a great day ahead')   # indicating end of schedule.

Using for loops we will iterate through whole data-frame in order to remind user of every task. If time is not specified then the loop will break. Then we are reading the time from the first row and cleaning it using our clean function. If time is not valid we will skip the current iteration. Then we will get the current time using datetime module. Then we will calculate tdelta(Time Delta) which means the time left for the next task to be reminded. after getting the time delta we will cover that hours and minutes into seconds. Using time.sleep(secs) the program will sleep until the specified seconds which are remaining for our next event to occur.

We are specifying the notify function to send notification in windows.title will be the title of the notification. app_icon is the path to the image which should be shown in the notification bar. You can specify any custom image in .ico format. timeout attribute defines after how much time the notification disappears. Then we will specify the speak function to speak the given lines.

Hence we built our own beginner level task-notifier project in python if you want full source code and many more projects in python the please checkout my GitHub Repository and if you want more such projects then please follow me. Hope! you enjoyed this :).