Learn how to read, write, and style Excel files using openpyxl. Discover practical examples for working with modern Excel formats (.xlsx) and advanced features.
openpyxl is an efficient Python library that enables you to read and write Excel files in the modern XML-based formats (.xlsx, .xlsm) introduced in Excel 2007, with full support for advanced features from Excel 2010 onward. It was originally developed as a Python port of PHPExcel and continues to be maintained by an active community of developers. The library excels at data automation, reporting, and formatting workflows, making it especially useful for users who need to work with Excel files but don't have Excel installed on their systems.
Many organizations use openpyxl for automating financial reports, generating charts from database exports, and applying formulas and formatting to marketing reports. This library offers fine-grained control over Excel files entirely through Python code.
What Is openpyxl?
openpyxl is designed specifically to provide native support for Excel 2010+ formats. The library works with several file formats:
.xlsx - Excel workbook
.xlsm - Excel workbook with macros
.xltx - Excel template
.xltm - Excel template with macros
One major advantage of openpyxl is that it doesn't require Excel to be installed on your computer. This makes it ideal for server environments, automated reporting systems, and data processing pipelines. Several other Python libraries, including pandas, use openpyxl as their Excel interface, which demonstrates its reliability and functionality.
Here's a practical example showing how to read data from an Excel file:
Load and explore a workbook
from openpyxl import load_workbook
# Load the workbook - use read_only=True for large files
wb = load_workbook('sample.xlsx', read_only=False, data_only=False)# data_only=True reads values instead of formulas
The optional parameters control how the workbook is loaded:
read_only=True improves performance for large files but limits editing capabilities
data_only=True returns calculated values instead of formulas
Iterate through data
To process multiple cells efficiently, use the iteration methods:
# Iterate through rowsfor row in sheet.iter_rows(min_row=1, max_row=5, values_only=True):print(row)# Returns a tuple of values# Iterate through columnsfor column in sheet.iter_cols(min_col=1, max_col=3, values_only=True):print(column)# Returns a tuple of values# Access a range of cells
cell_range = sheet["A1:C5"]for row in cell_range:for cell in row:print(cell.value)
You can easily transform Excel data into Python data structures:
# Convert worksheet data to a list of dictionariesdefsheet_to_dict(sheet):
data =[]
headers =[cell.value for cell in sheet[1]]# First row as headersfor row in sheet.iter_rows(min_row=2, values_only=True):
row_data ={}for key, value inzip(headers, row):
row_data[key]= value
data.append(row_data)return data
# Example usage
data_dict = sheet_to_dict(sheet)print(data_dict)
from openpyxl import load_workbook
# Load an existing workbook
wb = load_workbook("existing_file.xlsx")
sheet = wb.active
# Modify cell values
sheet["D1"]="Updated Data"
sheet.cell(row=5, column=2).value =42# Save to a new file (to preserve the original)
wb.save("modified_file.xlsx")
Note: When using load_workbook(), formulas are not automatically calculated by openpyxl. To see calculated values, use load_workbook(“file.xlsx”, data_only=True).
Insert charts
openpyxl can create various chart types:
from openpyxl import Workbook
from openpyxl.chart import BarChart, LineChart, Reference
wb = Workbook()
sheet = wb.active
# Add data
months =["Jan","Feb","Mar","Apr","May"]
values =[30,45,37,50,62]for i,(month, value)inenumerate(zip(months, values), start=1):
sheet.cell(row=i, column=1, value=month)
sheet.cell(row=i, column=2, value=value)# Create a bar chart
bar_chart = BarChart()
bar_chart.title ="Monthly Sales"
bar_chart.x_axis.title ="Month"
bar_chart.y_axis.title ="Sales"# Define the data range
data = Reference(sheet, min_col=2, min_row=1, max_row=5)
categories = Reference(sheet, min_col=1, min_row=1, max_row=5)# Add the data to the chart
bar_chart.add_data(data)
bar_chart.set_categories(categories)# Add the chart to the worksheet
sheet.add_chart(bar_chart,"D1")# Create a line chart on the same data
line_chart = LineChart()
line_chart.title ="Monthly Sales Trend"
line_chart.add_data(data)
line_chart.set_categories(categories)
sheet.add_chart(line_chart,"D15")
wb.save("charts.xlsx")제공
Insert Images
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
sheet = wb.active
# Add an image
img = Image("logo.png")# Resize the image (optional)
img.width =150
img.height =75# Add the image to cell A1
sheet.add_image(img,"A1")
wb.save("with_image.xlsx")제공
Note: You need the Pillow library installed to work with images: pip install pillow
Sheet, Row, and Column Management
openpyxl provides functions to manage the structure of your Excel files:
Creating and removing sheets
from openpyxl import Workbook
wb = Workbook()# Create new sheets
wb.create_sheet("Data")
wb.create_sheet("Summary",0)# Add at the beginning# Remove a sheet
wb.remove(wb["Sheet"])# Remove the default sheetprint(wb.sheetnames)# ['Summary', 'Data']
wb.save("multiple_sheets.xlsx")
from openpyxl import Workbook
wb = Workbook()
source = wb.active
source.title ="Original"# Add some data to copy
source["A1"]="Test Data"# Create a copy
wb.copy_worksheet(source)# The copied sheet will have "Copy of Original" name
wb.save("copied_sheet.xlsx")
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active
# Add some datafor i inrange(1,6):for j inrange(1,4):
sheet.cell(row=i, column=j, value=f"R{i}C{j}")# Insert a row at position 2
sheet.insert_rows(2)# Insert multiple rows
sheet.insert_rows(5,3)# Insert 3 rows at position 5# Insert a column at position 2
sheet.insert_cols(2)# Delete rows
sheet.delete_rows(7,2)# Delete 2 rows starting at row 7# Delete columns
sheet.delete_cols(3)# Delete column C
wb.save("modified_structure.xlsx")
While openpyxl is feature-rich, users have noted some limitations:
Limited support for named ranges
Documentation examples can be sparse for advanced features
Performance can be slow for very large files (though read-only mode helps)
Despite these limitations, openpyxl has strong community support. Issues can be reported on the GitLab tracker, and professional support is sometimes available. For more information and help, you can refer to:
openpyxl is a well-established library for Python-based Excel automation that offers complete support for reading, writing, and styling Excel files. It enables detailed control over spreadsheet output in automated workflows, from manipulating individual cells to creating charts and applying complex formatting.
For users who need to generate or analyze Excel files programmatically, particularly when building reports or processing tabular data, openpyxl provides an extensive toolkit. Its ability to work without requiring Excel installation makes it especially valuable for server environments and automated data pipelines.
Whether you're creating financial models, analyzing data, or generating reports, openpyxl gives you the tools to handle Excel files efficiently through Python code.
Vinod Chugani began his career in Tokyo as JPMorgan's youngest Hedge Fund Sales Desk Head and later set an individual sales record at Lehman Brothers, then built a 30-country electronics distribution business past SG$100 million in revenue before pivoting to data. A Duke Economics grad and NYC Data Science Academy alum, he was one of three scholarship recipients out of 100+ applicants for Hugo Bowne-Anderson's Building AI Applications course on Maven. Today, he writes for DataCamp, KDnuggets, Machine Learning Mastery, and Statology on topics from statistics to agentic AI, and mentors data professionals at NYC Data Science Academy with over 1,000 one-on-one sessions to his name.
openpyxl FAQs
What file types does openpyxl support?⌄
openpyxl supports several Excel formats, including .xlsx, .xlsm, .xltx, and xltm. It does not support the older .xls format used by Excel 2003 and earlier.
Can openpyxl read and evaluate Excel formulas?⌄
openpyxl can read and write formulas, but it does not evaluate them. To see formula results, the file must first be opened and saved in Excel.
Do I need Microsoft Excel installed to use openpyxl?⌄
No, openpyxl works entirely in Python and does not require Excel to be installed on your machine.
Can I insert images into Excel with openpyxl?⌄
Yes, but you must have the Pillow library installed, and the image file must exist on disk when calling add_image().
Is openpyxl suitable for large Excel files?⌄
Yes, especially when using read_only=True mode, which improves performance for reading large spreadsheets.
Learn to read and write Excel files in Python using openpyxl. This guide covers formulas, charts, cell formatting, and worksheet manipulation with hands-on exam
Learn how to effectively use VBA in Excel to automate tasks, create macros, and enhance your data processing skills with practical examples and best practices.
Learn how to automate repetitive Excel tasks with Excel macros. Create, use, and manage macros to boost productivity and transform your data workflows.
Learn about Python automation, including fundamental concepts, key libraries, working with data, using AI enhancements, and best practices. Includes real-world examples.
openpyxl is an efficient Python library that enables you to read and write Excel files in the modern XML-based formats (.xlsx, .xlsm) introduced in Excel 2007, with full support for advanced features from Excel 2010 onward. It was originally developed as a Python port of PHPExcel and continues to be maintained by an active community of developers. The library excels at data automation, reporting, and formatting workflows, making it especially useful for users who need to work with Excel files but don't have Excel installed on their systems.
Many organizations use openpyxl for automating financial reports, generating charts from database exports, and applying formulas and formatting to marketing reports. This library offers fine-grained control over Excel files entirely through Python code.
What Is openpyxl?
openpyxl is designed specifically to provide native support for Excel 2010+ formats. The library works with several file formats:
.xlsx- Excel workbook.xlsm- Excel workbook with macros.xltx- Excel template.xltm- Excel template with macrosOne major advantage of openpyxl is that it doesn't require Excel to be installed on your computer. This makes it ideal for server environments, automated reporting systems, and data processing pipelines. Several other Python libraries, including pandas, use openpyxl as their Excel interface, which demonstrates its reliability and functionality.
Installing and Importing openpyxl
Installing openpyxl is straightforward using pip:
For enhanced security when working with files from untrusted sources, you can also install the optional
defusedxmlpackage:To use openpyxl in your Python scripts, import the necessary classes:
Key Concepts and Terminology
To use openpyxl effectively, you need to understand the basic Excel structure:
In openpyxl, you can reference cells using either:
Excel style references:
sheet[“A1”]Row-column indexing:
sheet.cell(row=1, column=1)(Note: openpyxl uses 1-based indexing, not 0-based)Reading Excel Files with openpyxl
Here's a practical example showing how to read data from an Excel file:
Load and explore a workbook
The optional parameters control how the workbook is loaded:
read_only=Trueimproves performance for large files but limits editing capabilitiesdata_only=Truereturns calculated values instead of formulasIterate through data
To process multiple cells efficiently, use the iteration methods:
Convert to Python data structures
You can easily transform Excel data into Python data structures:
Writing Excel Files with openpyxl
Creating and modifying Excel files is equally straightforward with openpyxl.
Create a workbook and write basic data
Modify existing Excel files
Append data dynamically
The
.append()method makes it easy to add new rows:Formatting and Styling in Excel
openpyxl offers extensive options for cell formatting and styling.
Add fonts, borders, and alignment
Conditional formatting
Conditional formatting allows you to apply styles based on cell values:
Advanced Features
openpyxl supports many advanced Excel features that help with complex spreadsheet tasks.
Adding formulas
Note: When using
load_workbook(), formulas are not automatically calculated by openpyxl. To see calculated values, useload_workbook(“file.xlsx”, data_only=True).Insert charts
openpyxl can create various chart types:
Insert Images
Note: You need the Pillow library installed to work with images:
pip install pillowSheet, Row, and Column Management
openpyxl provides functions to manage the structure of your Excel files:
Creating and removing sheets
Renaming sheets
Copying worksheets
Inserting and deleting rows and columns
Freezing panes and adding filters
Limitations and Community Feedback
While openpyxl is feature-rich, users have noted some limitations:
Despite these limitations, openpyxl has strong community support. Issues can be reported on the GitLab tracker, and professional support is sometimes available. For more information and help, you can refer to:
Conclusion
openpyxl is a well-established library for Python-based Excel automation that offers complete support for reading, writing, and styling Excel files. It enables detailed control over spreadsheet output in automated workflows, from manipulating individual cells to creating charts and applying complex formatting.
For users who need to generate or analyze Excel files programmatically, particularly when building reports or processing tabular data, openpyxl provides an extensive toolkit. Its ability to work without requiring Excel installation makes it especially valuable for server environments and automated data pipelines.
Whether you're creating financial models, analyzing data, or generating reports, openpyxl gives you the tools to handle Excel files efficiently through Python code.