Skip to content

Instantly share code, notes, and snippets.

@palewire
Last active January 5, 2024 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palewire/381bd617d1d41e18b3b71f1755dbd9ed to your computer and use it in GitHub Desktop.
Save palewire/381bd617d1d41e18b3b71f1755dbd9ed to your computer and use it in GitHub Desktop.
How to open Excel files in Python without pandas

How to open Excel files in Python without pandas

The pandas data-analysis tool can do almost everything. And because it can do almost everything, it's a massive hunk of code. So you might not want to include it in your project if you can avoid taking on the weight.

One of pandas' handiest features is quickly opening Excel spreadsheets. If that's all you need, here's how to accomplish the task with openpyxl, a much lighter dependency.

import pandas as pd
# In pandas, it's one easy line. Pretty nice.
df = pd.read_excel("./example.xlsx")
from openpyxl import load_workbook
# Open up the Excel file.
workbook = load_workbook(filename="./example.xlsx")
# Get the first sheet.
worksheet = workbook.worksheets[0]
# Convert the sheet to a list of lists.
row_list = []
for r in worksheet.rows:
column = [cell.value for cell in r]
row_list.append(column)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment