Javatpoint Logo
Javatpoint Logo

Introduction to the pywhatkit Library

In this tutorial, we will learn about Python's pywhatkit module. As we know, Python provides numerous libraries and pywhatkit is one of them. The pywhatkit module is used to send the message by the Python script. Using this module, we can send the message to the desired number with a few lines of code. It uses WhatsApp web to send these messages.

Pywhatkit is a Python package for Whatsapp messenger written in Python 3.4+. It is quite simple and sophisticated.

Features of pywhatkit Library

The features of the pywhatkit library are given below.

  • Automating the process of sending messages to contacts or individuals on WhatsApp.
  • It can automatically send message to the group.
  • Initiating playback of a YouTube video or short.
  • It can also convert text(string) to handwriting
  • Sending Mails with HTML code.
  • It can perform a Google search.

Installation of Library

We need to install locally to work with the pywhatkit library because it will not come pre-installed in Python3. We can install it using the following command.

pip install pywhatkit

Sending WhatsApp Message

In this section, we will send the message to the Whatsapp automatically in simple way. Here one point is to remember that, to utilize WhatsApp in your browser, we must have an active login session and have previously set up WhatsApp Web in our preferred browser.

The pywhatkit library provides the sendmsg() method to send message automatically. Below is the syntax of sendmsg() method.

Syntax -

Parameters -

  • The first parameter is the receiver mobile which should be in the string format and the country code should be included as a prefix before the actual mobile number.
  • The message should be in string format.
  • Hours - The method follows a 24 hrs time format.
  • Minutes - It should be between 00-59.

Code Snippet

Let's understand the following code.

Example -

Explanation -

In the above code snippet, we import the pywhatkit library and use the sendwhatmsg() function to send a message to a phone number at a certain hour and minute. The code also uses exception handling to catch and display any errors that may occur while sending the message.

We can also send image with the caption as below -

We can send messages to the group using the following code.

We use this to send messages instantly in a group as if we write 0 hours, 0 minutes then it will send the message at 12:00 AM.

Play a YouTube Video

Python's pywhatkit provides the playont() method which is used to open the YouTube in our default browser and plays video as we passed in the argument. If we don't pass the topic name as parameter, it will play the random video. Otherwise, it opens same video as we pass URL in argument. Let's understand its syntax.

Syntax -

Parameters -

  • url/topic_name - It is given topic name or URL of the YouTube video.

Code Snippet

Let's understand the following example -

Example -

Explanation -

In the above code, we import the pywhatkit library and use the playonyt() function to play a random YouTube video based on the topic provided as a parameter. It also uses exception handling to catch and display any errors that may occur while playing the video, such as a network error.

Perform the Google Search

We can also use it for the google search using the search() method. It opens our browser and searches for the topic that we have provided in the code. Let's understand the following example.

Example -

Explanation -

In the above code, we use the PyWhatKit module to perform a Google search for the website "". The try-except block is used to handle any errors that may occur during the execution of the search function. If an error occurs, the except block will print an error message "An unknown error occurred".


In summary, this tutorial has covered using the PyWhatKit library in Python to perform automatic WhatsApp messaging. The example code demonstrated the use of the try-except block to handle any potential errors that may occur during the execution of the search function. The tutorial hopes that the reader has learned about using the PyWhatKit library and is ready to implement it in their code.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA