Get dynamic active range
Tags: #excel #openpyxl #active-range
Author: Florent Ravenel

Input

Import libraries

1
from openpyxl import load_workbook
2
from openpyxl.utils import get_column_letter
Copied!

Setup your Excel parameters

1
excel_path = "Excel_Template.xlsx"
Copied!

Model

Load Excel file and get active ws object

1
wb = load_workbook(excel_path)
2
ws = wb.active
3
ws
Copied!

Get active range

1
def get_active_range(ws):
2
max_row = ws.max_row
3
max_col = get_column_letter(ws.max_column)
4
active_range = f"A1:{max_col}{max_row}"
5
return active_range
6
7
active_range = get_active_range(ws)
Copied!

Output

Display result

1
active_range
Copied!
1
Copied!
Copy link
Edit on GitHub