How to Append Data in Excel Using Python
Last Updated :
28 Apr, 2025
We are given an Excel file and our task is to append the data into this excel file using Python. In this article, we'll explore different approaches to append data to an Excel file using Python.
Append Data in Excel Using Python
Below, are some examples to understand how to append data in excel using Python:
excel1.xlsx: Let's say we have an existing Excel file named 'excel1.xlsx' with the following data:
excel1.xlsxExample 1: Append Data In Excel Using Pandas Library
Before getting started, ensure that the pandas
library is installed. If not, you can install it using:
pip install pandas
In this example, below Python code uses the Pandas library to append new data (e.g., {'Name': ['Bob'], 'Age': [28], 'Salary': [55000]}) to an existing Excel file ('excel1.xlsx'). It reads the existing data into a DataFrame, appends the new data, and then saves the combined data back to the original Excel file.
Python3
import pandas as pd
# Existing Excel file
existing_file = 'excel1.xlsx'
# New data to append
new_data = {'Name': ['Bob'], 'Age': [28], 'Salary': [55000]}
df_new = pd.DataFrame(new_data)
# Read existing data
df_existing = pd.read_excel(existing_file)
# Append new data
df_combined = df_existing.append(df_new, ignore_index=True)
# Save the combined data to Excel
df_combined.to_excel(existing_file, index=False)
Output:
updatedExample 2: Append Data In Excel Using Openpyxl Library
If you don't have the openpyxl
library installed, you can install it using
pip install openpyxl
In this example, below Python code utilizes the openpyxl
library to load an existing Excel file ('existing_data.xlsx'), appends new data (e.g., ['Bob', 28, 55000]) to its active sheet, and saves the modified workbook back to the original file.
Python3
from openpyxl import load_workbook
# Existing Excel file
existing_file = 'excel1.xlsx'
# New data to append
new_data = [['Bob', 28, 55000]]
# Load existing workbook
wb = load_workbook(existing_file)
# Select the active sheet
ws = wb.active
# Append new data
for row in new_data:
ws.append(row)
# Save the workbook
wb.save(existing_file)
Output:
updatedConclusion
In conlcusion, Appending data to an existing Excel file using Python is a straightforward process with the help of libraries like pandas
and openpyxl
. Whether you prefer the convenience of pandas
for its DataFrame operations or the direct manipulation capabilities of openpyxl
, Python provides versatile tools for efficiently managing and updating Excel data. Choose the approach that best fits your workflow and data manipulation needs.
Similar Reads
How To Create A Csv File Using Python CSV stands for comma-separated values, it is a type of text file where information is separated by commas (or any other delimiter), they are commonly used in databases and spreadsheets to store information in an organized manner. In this article, we will see how we can create a CSV file using Python
3 min read
How to Add Numbers in a Csv File Using Python When working with CSV files in Python, adding numbers in the CSV file is a common requirement. This article will guide you through the process of adding numbers within a CSV file. Whether you're new to data analysis or an experienced practitioner, understanding this skill is vital for efficient data
3 min read
Copy Rows and Columns in Excel Using Python Manipulating data in Excel often involves tasks such as copying specific rows or columns based on certain conditions. Python, with its powerful libraries like Pandas and openpyxl, provides efficient solutions for handling Excel files. In this article, we will explore how to copy rows and columns in
3 min read
Convert Excel To Json With Python In the realm of data manipulation and analysis, the ability to convert data between different formats is a valuable skill. Excel and JSON are two widely used formats, and Python, with its powerful libraries, provides a seamless way to convert Excel files into JSON. In this article, we will see how w
4 min read
Append Multiple items to List - Python Appending items to a list in Python is an essential and common operation when working with different data structures. Sometimes, we need to add more than one item to a list at a time and in this article, we will explore the various ways to append multiple items to a list at the same time.Using exten
2 min read
Append Text or Lines to a File in Python Appending text or lines to a file is a common operation in programming, especially when you want to add new information to an existing file without overwriting its content. In Python, this task is made simple with built-in functions that allow you to open a file and append data to it. In this tutori
3 min read