Open In App

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.



Module needed:

pip install pandas
pip install pushbullet.py == 0.9.1
pip install openpyxl

Step-by-step implementation:

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:

  1. Go to your phone’s WhatsApp app.
  2. Select the user whose conversation you wish to export by clicking on his or her name.
  3. Now, in the upper right corner, click on the three dots.
  4. Select Export chats from the menu that appears when you click on more.
  5. After clicking on Export chats, a list of apps will appear, and you must select Pushbullet.
  6. 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.

Syntax: PushBullet(Your_Access_token)

Syntax:

pb.get_pushes() # Get all the Pushes 

all_pushes[0] # Get the latest pushes 

Syntax: urllib.request.urlretrieve(Chats_URL, Text_file_name)

Below is the full implementation:




# Import following modules
import urllib.request
import pandas as pd 
from pushbullet import PushBullet 
 
# Get Access Token from pushbullet.com
Access_token = "Your Access Token"
 
# Authentication
pb = PushBullet(Access_token) 
 
# All pushes created by you
all_pushes = pb.get_pushes() 
 
# Get the latest push
latest_one = all_pushes[0
 
# Fetch the latest file URL link
url = latest_one['file_url'
 
 
# Create a new text file for storing
# all the chats
Text_file = "All_Chats.txt" 
 
# Retrieve all the data store into
# Text file
urllib.request.urlretrieve(url, Text_file)
 
# Create an empty chat list
chat_list = [] 
 
# Open the Text file in read mode and
# read all the data
with open(Text_file, mode='r', encoding='utf8') as f:
   
     # Read all the data line-by-line
    data = f.readlines() 
 
# Excluded the first item of the list
# first items contains some garbage
# data
final_data_set = data[1:]
 
# Run a loop and read all the data
# line-by-line
for line in final_data_set:
      # Extract the date, time, name,
    # message
    date = line.split(",")[0
    tim = line.split("-")[0].split(",")[1
    name = line.split(":")[1].split("-")[1
    message = line.split(":")[2][:-1
     
    # Append all the data in a List
    chat_list.append([date, time, name, message])
 
# Create a dataframe, for storing
# all the data in a excel file
df = pd.DataFrame(chat_list,
                  columns = ['Date', 'Time',
                             'Name', 'Message'])
df.to_excel("BackUp.xlsx", index = False)

Output: 


Article Tags :