Excel files are widely used for data storage and manipulation. Python provides various libraries to work with Excel files, and one popular choice is OpenPyXL. OpenPyXL is a powerful library that allows you to read from and write to Excel files effortlessly. In this article, we will explore the basic steps to read from and write to an Excel file using OpenPyXL.
Before we start, make sure you have OpenPyXL installed on your Python environment. You can install it using pip by running the following command:
pip install openpyxl
Note: We have provided three sections, one for reading the data from the excel and second one is for writing the data to the excel file, in order to explore the
openpyxl
library without installing that on your local system.
To read from an Excel file, you need to create an instance of the load_workbook
class from OpenPyXL. After that, you can simply navigate to the specific cell from where you want to read the data.
In order to read multiple cells, you can use Python loops.
Here's a simple example that demonstrates how to read data from an existing Excel file:
import openpyxl# Load the workbookworkbook = openpyxl.load_workbook("employees.xlsx")# Identify active worksheetsheet = workbook.active# Identify the cellc = sheet.cell (row = 2, column = 1)# Access cell valueprint (c.value)
Here's a quick demonstration of how the employees.xlsx
file looks like:
Name | |
John | john@educative.io |
You can also fetch the maximum number of rows and columns in the excel file using the following code:
# Identify maximum rows countprint (sheet.max_row)# Identify maximum columns countprint (sheet.max_column)
Let's see a working example of reading from an Excel file. Execute the following code by clicking the "Run" button:
UEsDBBQACAgIAB0Z51YAAAAAAAAAAAAAAAAYAAAAeGwvZHJhd2luZ3MvZHJhd2luZzEueG1sndBdbsIwDAfwE+wOVd5pWhgTQxRe0E4wDuAlbhuRj8oOo9x+0Uo2aXsBHm3LP/nvzW50tvhEYhN8I+qyEgV6FbTxXSMO72+zlSg4gtdgg8dGXJDFbvu0GTWtz7ynIu17XqeyEX2Mw1pKVj064DIM6NO0DeQgppI6qQnOSXZWzqvqRfJACJp7xLifJuLqwQOaA+Pz/k3XhLY1CvdBnRz6OCGEFmL6Bfdm4KypB65RPVD8AcZ/gjOKAoc2liq46ynZSEL9PAk4/hr13chSvsrVX8jdFMcBHU/DLLlDesiHsSZevpNlRnfugbdoAx2By8i4OPjj3bEqyTa1KCtssV7ercyzIrdfUEsHCAdiaYMFAQAABwMAAFBLAwQUAAgICAAdGedWAAAAAAAAAA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SwECFAAUAAgICAAdGedWsjqyvNUBAACuBAAADQAAAAAAAAAAAAAAAACmCAAAeGwvc3R5bGVzLnhtbFBLAQIUABQACAgIAB0Z51ZNyqKtRwEAACYDAAAPAAAAAAAAAAAAAAAAALYKAAB4bC93b3JrYm9vay54bWxQSwECFAAUAAgICAAdGedWlhnBU+oAAAC5AgAAGgAAAAAAAAAAAAAAAAA6DAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJlbHNQSwECFAAUAAgICAAdGedWpG+hILIAAAAoAQAACwAAAAAAAAAAAAAAAABsDQAAX3JlbHMvLnJlbHNQSwECFAAUAAgICAAdGedWbYi0UDUBAAAZBAAAEwAAAAAAAAAAAAAAAABXDgAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLBQYAAAAACgAKAJoCAADNDwAAAAA=
To write to an Excel file, you need to create an instance of the Workbook
class from OpenPyXL. Here's a simple example that demonstrates how to create a new Excel file and write data to it:
from openpyxl import Workbook# Create a new workbookworkbook = Workbook()# Select a specific sheetsheet = workbook.active# Write data to cellssheet['A1'] = 'Hello'sheet['B1'] = 'World'# Save the workbookworkbook.save('new_file.xlsx')
In the above code, we create a new workbook using Workbook
and access the active sheet. Then, we write data to specific cells by assigning values to them. Finally, we save the workbook to a new Excel file using the save
method.
Let's see a working example of writing to an Excel file. Execute the following code by clicking the "Run" button:
UEsDBBQACAgIAB0Z51YAAAAAAAAAAAAAAAAYAAAAeGwvZHJhd2luZ3MvZHJhd2luZzEueG1sndBdbsIwDAfwE+wOVd5pWhgTQxRe0E4wDuAlbhuRj8oOo9x+0Uo2aXsBHm3LP/nvzW50tvhEYhN8I+qyEgV6FbTxXSMO72+zlSg4gtdgg8dGXJDFbvu0GTWtz7ynIu17XqeyEX2Mw1pKVj064DIM6NO0DeQgppI6qQnOSXZWzqvqRfJACJp7xLifJuLqwQOaA+Pz/k3XhLY1CvdBnRz6OCGEFmL6Bfdm4KypB65RPVD8AcZ/gjOKAoc2liq46ynZSEL9PAk4/hr13chS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AAASwQAAHhsL3RoZW1lL3RoZW1lMS54bWxQSwECFAAUAAgICAAdGedWLYxdpLAAAAD9AAAAFAAAAAAAAAAAAAAAAAC0BwAAeGwvc2hhcmVkU3RyaW5ncy54bWxQSwECFAAUAAgICAAdGedWsjqyvNUBAACuBAAADQAAAAAAAAAAAAAAAACmCAAAeGwvc3R5bGVzLnhtbFBLAQIUABQACAgIAB0Z51ZNyqKtRwEAACYDAAAPAAAAAAAAAAAAAAAAALYKAAB4bC93b3JrYm9vay54bWxQSwECFAAUAAgICAAdGedWlhnBU+oAAAC5AgAAGgAAAAAAAAAAAAAAAAA6DAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJlbHNQSwECFAAUAAgICAAdGedWpG+hILIAAAAoAQAACwAAAAAAAAAAAAAAAABsDQAAX3JlbHMvLnJlbHNQSwECFAAUAAgICAAdGedWbYi0UDUBAAAZBAAAEwAAAAAAAAAAAAAAAABXDgAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLBQYAAAAACgAKAJoCAADNDwAAAAA=
If you want to modify an existing Excel file, you can load it using load_workbook
and manipulate its content. Here's an example that demonstrates how to update the value of a cell in an existing Excel file:
from openpyxl import load_workbook# Load the workbookworkbook = load_workbook('employees.xlsx')# Select a specific sheetsheet = workbook.active# Update cell valuesheet['A1'] = 'New Value'# Save the workbookworkbook.save('employees.xlsx')
In the above code, we load the existing Excel file, update the value of a specific cell (A1
in this case), and save the modified workbook back to the same file.
Let's see a working example of modifying existing Excel files. Execute the following code by clicking the "Run" button:
UEsDBBQACAgIAB0Z51YAAAAAAAAAAAAAAAAYAAAAeGwvZHJhd2luZ3MvZHJhd2luZzEueG1sndBdbsIwDAfwE+wOVd5pWhgTQxRe0E4wDuAlbhuRj8oOo9x+0Uo2aXsBHm3LP/nvzW50tvhEYhN8I+qyEgV6FbTxXSMO72+zlSg4gtdgg8dGXJDFbvu0GTWtz7ynIu17XqeyEX2Mw1pKVj064DIM6NO0DeQgppI6qQnOSXZWzqvqRfJ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dvcmtzaGVldHMvX3JlbHMvc2hlZXQxLnhtbC5yZWxzUEsBAhQAFAAICAgAHRnnVmWjgWEoAwAArQ4AABMAAAAAAAAAAAAAAAAASwQAAHhsL3RoZW1lL3RoZW1lMS54bWxQSwECFAAUAAgICAAdGedWLYxdpLAAAAD9AAAAFAAAAAAAAAAAAAAAAAC0BwAAeGwvc2hhcmVkU3RyaW5ncy54bWxQSwECFAAUAAgICAAdGedWsjqyvNUBAACuBAAADQAAAAAAAAAAAAAAAACmCAAAeGwvc3R5bGVzLnhtbFBLAQIUABQACAgIAB0Z51ZNyqKtRwEAACYDAAAPAAAAAAAAAAAAAAAAALYKAAB4bC93b3JrYm9vay54bWxQSwECFAAUAAgICAAdGedWlhnBU+oAAAC5AgAAGgAAAAAAAAAAAAAAAAA6DAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJlbHNQSwECFAAUAAgICAAdGedWpG+hILIAAAAoAQAACwAAAAAAAAAAAAAAAABsDQAAX3JlbHMvLnJlbHNQSwECFAAUAAgICAAdGedWbYi0UDUBAAAZBAAAEwAAAAAAAAAAAAAAAABXDgAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLBQYAAAAACgAKAJoCAADNDwAAAAA=
OpenPyXL provides many other functionalities to work with Excel files, such as formatting cells, merging cells, adding charts, and more. By using the examples and exploring the OpenPyXL documentation, you can perform various operations on Excel files with ease.
In conclusion, OpenPyXL is a powerful library that simplifies the process of reading from and writing to Excel files in Python. With its intuitive interface and extensive capabilities, you can effortlessly manipulate Excel data and automate various tasks.