Introduction to Python OpenPyxl
Last Updated :
09 Dec, 2025
OpenPyxl is a Python library that provides tools for reading, writing, and manipulating Excel files in the Excel 2010 format (which uses the .xlsx extension). This is a high-level library that abstracts away many low-level details, making it simple to work with spreadsheets programmatically.
- Read and Write Excel Files: OpenPyxl can be used to both read from and write to .xlsx files.
- Work with Cells: We can access, modify, and create new cells in Excel sheets.
- Support for Formulas: OpenPyxl supports the evaluation of Excel formulas.
- Styles and Formatting: We can modify the look and feel of cells (font, color, borders, etc.).
- Charting: OpenPyxl also provides the ability to create simple charts in Excel files.
- Working with Pivot Tables: Though limited, OpenPyxl offers the capability to interact with pivot tables.
Installing OpenPyxl
To use OpenPyxl, we'll first need to install it. We can do this using pip:
pip install openpyxl
Once installed, we're ready to start working with Excel files.
Basic Usage of OpenPyxl
1. Creating a New Excel File
Creating a new Excel file and saving it is quite straightforward with OpenPyxl. Here’s how to create a basic Excel workbook and sheet:
Python
import openpyxl
# Create new workbook
wb = openpyxl.Workbook()
# Access active sheet
sheet = wb.active
sheet.title = "Sample Sheet"
# Save workbook
wb.save("sample_workbook.xlsx")
Explanation:
- Workbook() creates a new Excel file.
- .active returns the default sheet.
- .save() writes the file to disk.
2. Writing Data to an Excel File
Let's now populate the Excel sheet with some data:
Python
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet["A1"] = "Name"
sheet["B1"] = "Age"
sheet["A2"] = "Alice"
sheet["B2"] = 25
sheet["A3"] = "Bob"
sheet["B3"] = 30
wb.save("data_workbook.xlsx")
Output:
Writing in a excel fileExplanation:
- Data is written directly by referencing cell names (e.g., "A1").
- Creates a simple table with names and ages.
3. Reading Data from an Excel File
Reading data from an Excel file is equally simple. Let’s read the data we wrote in the previous example:
Python
import openpyxl
wb = openpyxl.load_workbook("data_workbook.xlsx")
sheet = wb.active
for row in sheet.iter_rows(min_row=1, max_row=3, values_only=True):
print(row)
Output:
Reading Excel File in OpenPyxlExplanation:
- load_workbook() opens an existing file.
- iter_rows() retrieves rows as tuples.
- values_only=True returns only cell values, not cell objects.
4. Modifying an Existing Excel File in OpenPyxl
We can easily modify existing Excel files by loading them and then editing the content:
Python
import openpyxl
wb = openpyxl.load_workbook("data_workbook.xlsx")
sheet = wb.active
sheet["B2"] = 26 # Update age
wb.save("data_workbook_modified.xlsx")
Output:
Modify Excel file in OpenpyxlExplanation: Load an existing file → update cells → save as a new file.
OpenPyxl allows us to apply styles and formatting to cells, such as changing the font size, color, or applying bold formatting:
Python
from openpyxl import Workbook
from openpyxl.styles import Font
wb = Workbook()
sheet = wb.active
sheet["A1"] = "Formatted Text"
sheet["A1"].font = Font(size=14, bold=True, color="FF0000")
wb.save("formatted_workbook.xlsx")
Output:
Format cell in OpenpyxlExplanation:
- Use Font() to set bold text, size, and color.
- Formats are applied directly to cell objects.
OpenPyxl also supports adding Excel formulas. We can assign a formula to a cell just like any other value:
Python
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet["A1"] = 10
sheet["A2"] = 20
sheet["A3"] = "=A1 + A2" # Formula
wb.save("formula_workbook.xlsx")
Output:
Adding formula to cells in OpenPyxlExplanation:
- Assign formulas as plain strings.
- Excel evaluates the formula when the file is opened.
7. Merging and Unmerging Cells in OpenPyxl
We can merge and unmerge cells in Excel using OpenPyxl. Here's how to merge and unmerge a range of cells:
Python
import openpyxl
# Create a new Workbook
wb = openpyxl.Workbook()
# Select the active sheet
sheet = wb.active
# Merge cells from A1 to B2
sheet.merge_cells("A1:B2")
# Write data in the merged cell
sheet["A1"] = "Merged Cells"
# Save the workbook
wb.save("merged_workbook.xlsx")
Output:
Merge Cells in OpenPyxlExplanation:
- merge_cells() merges a specified range.
- Data is written to the top-left cell of the merged region.
Related Articles:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice