Home Python If Else For Loop Function Array String Regex
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 -
pywhatkit.sendmsg("receiver mobile number", "message", hours, m
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 -
import pywhatkit as pwk
try:
# Sending message in WhatsApp in India, using Indian dial code
pwk.sendwhatmsg("+91XXXXXX5980", "Hi, how are you?", 20, 3
print("Message Sent!") # Prints success message in console
except Exception as e:
print(f"Error in sending the message: {e}")
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 -
pwk.sendwhats_image("Group_Name", "Media/image.png", "Hi")
pwk.sendwhats_image("Name", "Media/images.png")
We can send messages to the group using the following code.
pwk.sendwhatmsg_to_group("Group_Name", "Hey Guys! How's ev
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.
pwk.sendwhatmsg_to_group_instantly("Group_Name", "Hey Guys
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 -
pywhatkit.playonyt("url/topic name")
Parameters -
url/topic_name - It is given topic name or URL of the
YouTube video.
Code Snippet
Let's understand the following example -
Example -
# importing the module
import pywhatkit
# using Exception Handling
# to avoid exceptions
try:
# it plays a random YouTube
# video of Javatpoint
pywhatkit.playonyt("Javatpoint")
print("Playing...")
except:
# printing the error message
print("Network Error Occurred")
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 -
# importing the module
import pywhatkit
# use Try Except to
# handle the Exceptions
try:
# it will perform the Google search
pywhatkit.search("javatpoint.com")
print("Searching...")
except:
# Printing Error Message
print("An unknown error occurred")
Explanation -
In the above code, we use the PyWhatKit module to perform a
Google search for the website "javatpoint.com". 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".
Conclusion
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.
← Prev Next →
For Videos Join Our Youtube Channel: Join
Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Splunk SPSS
Swagger Transact-SQL
Tumblr ReactJS
Regex Reinforcement
Learning
R Programming RxJS
React Native Python Design
Patterns
Python Pillow Python Turtle
Keras
Preparation
Aptitude Reasoning
Verbal Ability Interview
Questions
Company
Questions
Trending Technologies
Artificial AWS
Intelligence
Selenium Cloud Computing
Hadoop ReactJS
Data Science Angular 7
Blockchain Git
Machine DevOps
Learning
B.Tech / MCA
DBMS Data Structures
DAA Operating
System
Computer Compiler Design
Network
Computer Discrete
Organization Mathematics
Ethical Hacking Computer
Graphics
Software Web Technology
Engineering
Cyber Security Automata
C Programming C++
Java .Net
Python Programs
Control System Data Mining
Data Warehouse