Skip to main content

Command Palette

Search for a command to run...

A Python Module PyWhatKit for Automation

Published
4 min readView as Markdown
A Python Module PyWhatKit for Automation

PyWhatKit is a Python module that allows you to schedule tasks or notifications to be displayed at a specific time in the future. This is useful if you want to be reminded of something or if you want to automate a task to be performed at a specific time.

To use PyWhatKit, you will need to install it using

pip install pywhatkit.

Once it is installed, you can import it into your Python code using

import pywhatkit.

To schedule a task or notification, you can use the sendwhatmsg function. This function takes four arguments: the hour, the minute, the message, and the website to open (optional). For example, to schedule a notification for 10:30 am that says “Time for a break!”, you can use the following code:

import pywhatkit

pywhatkit.sendwhatmsg(10, 30, "Time for a break!")

You have the option to schedule a task to open a website at a designated time. For instance, using the following code, you can schedule web.whatsapp.com to open at 12:00 am.

import pywhatkit

pywhatkit.sendwhatmsg(12, 0, "Open website", "https://web.whatsapp.com/")

In addition to the sendwhatmsg function, PyWhatKit also provides several other functions for scheduling tasks and notifications. For example, you can use the playonyoutube function to play a specific YouTube video at a specific time, or the alarm function to set an alarm for a specific time.

In summary, PyWhatKit is a beneficial and handy Python library for scheduling future tasks and alerts. It's user-friendly and can enhance any Python project that necessitates automated tasks or notifications.

The following example code illustrates how to utilize the PyWhatKit library to schedule a reminder for 10:30 am that says "Time for a meeting!

import pywhatkit

# Schedule the notification for 10:30am
pywhatkit.sendwhatmsg(10, 30, "Time for a meeting!")

print("Notification scheduled for 10:30am")

This code will schedule the notification to be displayed at 10:30am. You can change the hour and minute values to schedule the notification for a different time.

You can also add additional options to customize the notification. For example, you can use the speak argument to have the notification read aloud by your computer’s text-to-speech function. You can also use the interval argument to repeat the notification at regular intervals.

Here is an example code that demonstrates how to use these additional options:

import pywhatkit

# Schedule the notification for 10:30am and have it repeat every 30 minutes
pywhatkit.sendwhatmsg(10, 30, "Time for a meeting!", speak=True, interval=30)

print("Notification scheduled for 10:30am and will repeat every 30 minutes")

To send a message using the PyWhatKit module, you will need to use the sendwhatmsg function and specify the phone number and message as arguments.

Utilize pywhatkit for automating the process of sending WhatsApp messages.

Here is an example code that demonstrates how to send a message to the phone number +91xxxxxxxxxx with the message “Hello, how are you?” using PyWhatKit:

Note: You can give the mobile number according to the person number that you are willing to send a WhatsApp message

import pywhatkit

# Send the message at 11:00am
pywhatkit.sendwhatmsg(11, 0, "Hello, how are you?", "+91xxxxxxxxxx")

print("Message scheduled for delivery at 11:00am")

It's important to note that PyWhatKit does not provide a direct mechanism for sending WhatsApp messages. Rather, it utilizes your computer's web browser to open a pre-composed message on the WhatsApp website. Therefore, you need to have WhatsApp web open on your browser for the message to be sent.

Now Let’s update our code and add some new features, such as collecting users input. Let’s move on it and add some features to our existing Python code:

import pywhatkit

while True:

    print("please type the phone number with country code \n ")

    number = input("Enter the Reciever number to send message \n >>>")

    try:
        value = int(number)

        message = input("Enter the message you want to send \n >>>")

        time = input("Enter the time you want to send the message \n >>>")

        min = input("Enter in which min you want to send the message \n >>>")

        break;

    except ValueError:

        print("This is not a number. Please enter a valid number")

pywhatkit.sendwhatmsg(number,message,int(time),int(min))

Let’s play a youtube video with this module

The pywhatkit.playonyt() command can be used to open your default browser on YouTube and play the specified video. If you pass the topic name as a parameter, it will play a random video on that subject. If you pass the URL as a parameter, it will open the specific video.

Now, let's proceed to the next step which can be achieved by using a specific command.

You can use pywhatkit.playonyt(“Open Source”) This will start playing the first video that comes up when you search for “Open Source” on YouTube.

import pywhatkit

pywhatkit.playonyt("Open Source")

More from this blog

Untitled Publication

17 posts