Locked lesson.
About this lesson
To load data from an existing Excel spreadsheet, we need to import load_workbook from openpyxl.
Exercise files
Download this lesson’s related exercise files.
14 - Load Existing Excel File and Grab One Cell.docx57.2 KB 14 - Load Existing Excel File and Grab One Cell SOLUTION.docx
56.1 KB
Quick reference
Load Existing Excel File and Grab One Cell
To load data from an existing Excel spreadsheet, we need to import load_workbook from openpyxl.
When to use
Do this any time you want to load data from an existing Excel spreadsheet into your Python program.
Instructions
# import the load_workbook library
from openpyxl import load_workbook
# Load the data from Excel
wb = load_workbook('filename.xlsx')
# Grab the active sheet
ws = wb.active
# Grab a specific cell from the spreadsheet
cell = ws['A2']
# print the value of that cell
print(cell.value)
# Change the value of A2 to something else
ws['A2'] = "Something Else"
# Save our changed data to a spreadsheet
wb.save('filename.xlsx')
Hints & tips
- from openpyxl import load_workbook
- wb = load_workbook('filename.xlsx')
- cell = ws['A2']
- print(cell.value)
- ws['A2'] = "Something Else"
- wb.save('filename.xlsx')
Lesson notes are only available for subscribers.