Export WhatsApp Chat History to Excel using Python
What is WhatsApp chat History?
WhatsApp is one of the most popular instant messaging applications available on smartphones and desktop computers. It is an easy-to-use platform for communication with friends, family, and colleagues. Saving chat history is one of the useful abilities of WhatsApp. Due to this feature, we can use the recorded or saved record of conversations if needed.
WhatsApp chat history is essentially a record of all the conversations that have taken place on the platform. The chat history includes all messages sent and received by the user, including text messages, photos, videos, voice messages, and other types of media. The chat history is saved on the user's device, and if the user backs up their chat history to the cloud, it can be accessed from any device.
WhatsApp chat history can be a useful tool for a variety of reasons. For example, it can help users keep track of important information, such as meeting times, addresses, and phone numbers. Additionally, it can be used to remember past conversations and help users maintain a sense of continuity in their relationships. Furthermore, it can be a useful tool for businesses, as it can help track customer interactions and provide a record of customer service conversations.
To access WhatsApp chat history, users simply need to open the app and navigate to the chat they wish to view. The chat history will be displayed, and the user can scroll up and down to view previous messages. If the user wants to search for a specific message or keyword, they can use the search function, which is accessible by tapping on the magnifying glass icon in the top right corner of the chat window.
Users can also customize their chat history settings to their liking. For example, they can choose how far back their chat history goes, how often their chat history is backed up, and whether or not they want to save media files, such as photos and videos. If the user wants, he can delete the chat history or vice versa.
It is important to note that WhatsApp chat history is not completely secure. While the platform uses end-to-end encryption to ensure that messages can only be seen by the sender and recipient, there are still ways for others to gain access to chat history. For example, if someone gains access to the user's device, they may be able to view the chat history. Additionally, if the user backs up their chat history to the cloud, it may be accessible to others if they gain access to the user's cloud storage account.
What is Excel?
Microsoft Excel is a powerful spreadsheet program that is widely used in businesses, educational institutions, and other organizations. It is a part of the Microsoft Office suite and is available for Windows, Mac, and mobile devices. Excel provides a vast range of features and functions that enable users to organize, analyse, and visualize data in a variety of ways.
One of the primary functions of Excel is to create and manipulate spreadsheets. Spreadsheets are essentially tables of data that can be used to organize information, perform calculations, and display results. In Excel, users can create and edit spreadsheets with ease and can use a variety of tools and functions to analyse and manipulate data.
Excel is a versatile program that can be used for a wide range of tasks. For example, it can be used to manage budgets, track expenses, analyse financial data, create graphs and charts, and much more. The program has a range of built-in functions, such as SUM, AVERAGE, MAX, and MIN, that enable users to quickly perform calculations and analyse data.
Excel contains a lot of useful features, one of which is to create graphs and charts. Charts and graphs are visual representations of data and can be used to help users understand and interpret complex data sets. Excel provides a range of chart types, including bar charts, line charts, pie charts, and scatter plots, and users can customize the appearance of their charts to suit their needs.
Excel can also be used to analyse the data, features like conditional formatting and pivot tables are available. Pivot tables enable users to quickly analyse large data sets and summarize them in a variety of ways, while conditional formatting enables users to highlight important data points or trends in their spreadsheets.
Excel is a highly customizable program, and users can create their own macros and functions to automate repetitive tasks. This can save a great deal of time and effort, especially for users who work with large or complex data sets on a regular basis.
One of the advantages of Excel is its widespread use in the business world. Many businesses use Excel to manage financial data, analyse trends, and make informed decisions. As a result, proficiency in Excel is often a valuable skill for job seekers, and many employers specifically seek out candidates with strong Excel skills.
Exporting WhatsApp chat Data:
Before we dive into the code, we need to ensure that we have the required tools and libraries installed on our system. We will be using Python 3, Pandas, and SQLite to extract the data and store it in a CSV file.
Step 1: Extracting the Chat Data
To extract the chat data from WhatsApp, we need to create a backup of the chat history. This backup can be created by going to the WhatsApp settings and selecting “Chats” and then “Chat backup.” Once the backup is created, we need to transfer it to our computer.
Next, we need to extract the data from the backup file. We can use the open-source tool called “WhatsApp Viewer” to extract the data. This tool can be downloaded from the following link: https://github.com/andreas-mausch/whatsapp-viewer.
Step 2: Storing the Data in a SQLite Database
Once we have extracted the chat data, we need to store it in a SQLite database. SQLite is a lightweight database that is perfect for small to medium-sized projects. We can use the “sqlite3” module in Python to connect to the database and store the data.
We first need to create a database and a table to store the data. We can do this using the following code:
import sqlite3 conn = sqlite3.connect('whatsapp_chat.db') c = conn.cursor() c.execute('''CREATE TABLE chat_data (message_id INTEGER PRIMARY KEY, message_text TEXT, message_date TEXT, sender TEXT)''')
We create a new SQLite database called “whatsapp_chat.db” and create a new table called “chat_data” with columns for the message ID, message text, message date, and sender. Next, we need to read the data from the extracted chat file and insert it into the database. We can do this using the following code:
import os import sys from datetime import datetime from yowsup.demos.zclient.cli import WhatsappCli from yowsup.layers.protocol_messages.protocolentities.message_text import TextMessageProtocolEntity def store_messages(message, phone): sender = message.getFrom(full=False) message_date = message.getTimestamp() message_text = message.getBody() c.execute("INSERT INTO chat_data (message_id, message_text, message_date, sender) VALUES (?,?,?,?)", (message_id, message_text, message_date, sender)) conn.commit()
We use the “yowsup” library to read the data from the extracted chat file and insert it into the “chat_data” table.
Step 3: Exporting the Data to Excel
Once we have stored the data in the SQLite database, we can use the “pandas” library to export the data to a CSV file. We can do this using the following code:
import pandas as pd df = pd.read_sql_query("SELECT * from chat_data", conn) df.to_csv('whatsapp_chat.csv', index=False)
We use the “read_sql_query” function in Pandas to read the data from the “chat_data” table, and then use the “to_csv” function to export the data to a CSV file.
Conclusion
Exporting WhatsApp chat data to Excel using Python can save you time and effort, and also provides more control over the exported data. With the help of Python libraries such as Pandas and SQLite, we can quickly extract, store, and export WhatsApp chat data to Excel. This can be useful for businesses that want to analyse their customer conversations and gain insights into their behavior and preferences. Overall, using Python to export WhatsApp chat data can be an efficient and effective way to manage and analyse customer data.