Home »
Python
Alarm clock in Python with the help of datetime and pyttsx3
In this tutorial, we are going to learn how to create an alarm clock in Python with the help of datetime and pyttsx3 modules?
Submitted by Abhinav Gangrade, on June 04, 2020
Modules used:
To create this script, we will use 3 modules:
datetime module: datetime is an inbuilt python module which will help us to provide some function which will deal with the date and time, we will use this module just to get the actual standard time.
time module: time is an inbuilt python module that provides us with various time-related functions and we are using this module just to sleep the program for 1 second.
pyttsx3: pyttsx3 is a python module that will help us to convert the input text into speech.
We can install in 2 ways:
-
Using the command:
pip install pyttsx3
-
Pycharm users: Go to the project interpreter and install this module from there
Note: This alarming interface is based on 24 hours clock format.
How we will do this?
We have to use a loop for this interface.
We will use the datetime module to get the actual standard time.
Now if the users time matched with the standard time then we have to alarm the user, for this, we will use the pyttsx3 module inside an if condition statement and we will apply the various function of pyttsx3 inside the if condition.
Now the function that we will in the script are:
- datetime.datetime.now(): This will give us the exact date and standard time.
- datetime.datetime.now().strftime("%H:%M"): Here we only work with time, to get the time we will use this function.
- time.sleep(1): we have to run a loop in every second so we will stop the functioning of the program.
Regarding the pyttsx3:
When the standard time matches with the users time then we have to alarm the user with the help of pyttsx3, the functions that we will use are:
- pyttsx3.init():This function will create a engine and all the other function
- engine.say(): This function will convert the text to speech.
- engine.runAndWait(): This will make the speech audible.
Program:
# import libraries(datetime,time,pyttsx3)
import time,pyttsx3
from datetime import datetime
print("Hello, welcome to my alarming interface...")
print("It is based on 24 hours clock format...")
Name = input("Enter your name: ")
# printing the current time
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time is: ", current_time)
# Enter the time(Hours and minute)
Time=input("Enter the time (HH24:MM): ")
# starting the loop
while True:
# Getting the standard time with the
# help of datetime module
Standard_time=datetime.now().strftime("%H:%M")
# sleep the program for about 1 sec
time.sleep(1)
# if condition to check wether the input
# time has matched or not
if Time==Standard_time:
count=0
while count<=10:
count=count+1
# creating a engine for voice mode
engine=pyttsx3.init()
# setting the voice
engine.say("Wake up"+Name)
engine.runAndWait()
print("Thankyou For using the Interface")
break
Output:
Hello, welcome to my alarming interface...
It is based on 24 hours clock format...
Enter your name: Abhinav
Current Time is: 00:33:38
Enter the time (HH24:MM): 00:35
This is the way we can create an Alarming clock script in python.