Export WhatsApp Chat History to Excel Using Python
In this article, we will discuss how to export a specific user’s chats to an Excel sheet. To export the chats, we will use several Python modules and libraries.
In the Excel file, we will create four columns: Date, Time, Name, and Message. We’ll create these columns through Pandas, export all the chat details to their respective columns, and use Pushbullets to retrieve the data using the API key.
- Pandas: Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series.
pip install pandas
pip install pushbullet.py == 0.9.1
- openpyxl: It is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let’s see how to perform different arithmetic operations using openpyxl.
pip install openpyxl
Step 1: Set up a Pushbullet account on your PC and Phone
Step 2: Install the Pushbullet app on your phone. Log in using the same email address that you used to log in to your PC.
Step 3: After creating an account on both the computer and the phone.
Navigate to the Devices option in the top left corner of your PC. And then select Add a device, and add your phone. When you add your phone, the name of your phone will be shown there.
Step 4: Now export the specific user’s conversations.
Follow these steps:
- Go to your phone’s WhatsApp app.
- Select the user whose conversation you wish to export by clicking on his or her name.
- Now, in the upper right corner, click on the three dots.
- Select Export chats from the menu that appears when you click on more.
- After clicking on Export chats, a list of apps will appear, and you must select Pushbullet.
- You may now see a text file on Pushbullet.com on your PC that contains the chats.
Step 5: Now, get the API key from Pushbullet.com
Open Pushbullet.com on your PC and Go to Settings Then scroll down and click on Access Tokens. Click on Create Access Token and copy the Token
Step 6: Write the code for export chat into excel.
- Using PushBullet, authenticate the key now. An InvalidKeyError is thrown if your key is invalid (the Pushbullet API returns 401).
- You may obtain all of your past pushes produced by you, which implies all of the files you submitted to Pushbullet, by using get_pushes. However, we want the most recent push, which will appear at index ‘0‘ in a list of all pushes. Now that we’ve received the most recent push, we need to retrieve the URL of our export conversations from Pushbullet, so just give the file_url.
pb.get_pushes() # Get all the Pushes
all_pushes # Get the latest pushes
- Create a text file to save all of the chats, and then use the urlretrieve method to retrieve all of the data from the URL. urlretrieve accepts two arguments: the URL and the text file in which to save all of the data.
Syntax: urllib.request.urlretrieve(Chats_URL, Text_file_name)
- Open the text file and read all of the data lines by line, using the utf8 encoding to encode the data. It will save the content of the text file in a list format after reading it from the text file. Because index ‘0‘ contains some trash text, we must delete it using list slicing.
- Run a loop to extract the Date, Time, Name, and Message from the file data using list slicing. Simply add them to a list once they’ve been extracted.
- Finally, use the Pandas library to construct a data frame that will be used to store all of the data on an Excel sheet. pd.DataFrame accepts two arguments: a list of text and the columns to be created on an excel sheet. After that, just use the to_excel method to save them to an excel file.
Below is the full implementation: