Python Read Large Excel xlsx File in Read Only Mode using OpenPyXL
In this Python openpyxl tutorial we learn how to read a large excel file with the read only mode of the openpyxl package. This read only mode allows optimised performance during reading large Excel files.
How to install the openpyxl package
To install the openpyxl package using the command below.
pip install openpyxl
How to read Excel xlsx file using read only mode
For example we have an Excel file named large_file.xlsx as below screenshot.
Import the load_workbook from the openpyxl package.
from openpyxl import load_workbook
Read the Excel file into a Workbook object with read_only is True.
wb = load_workbook(filename='large_file.xlsx', read_only=True)
Access the Worksheet.
ws = wb['sheet1']
Read cell values and print it out.
for row in ws.rows:
for cell in row:
print(cell.value)
Close the Workbook.
wb.close()
The complete Python program as below.
read_only_xlsx.py
from openpyxl import load_workbook
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['sheet1']
for row in ws.rows:
for cell in row:
print(cell.value)
wb.close()
Execute the above Python program we will get the output as below.
Column 1
Column 2
1
30
2
29
3
28
4
27
5
26
6
....
Happy Coding 😊