How to get the first sheet of an excel workbook using openpyxl?

Viewed 1495

I'm able to get the desired sheet by using wb["sheet_name"] method but I want to get the first, or let's say the nth sheet, regardless of the name.

wb = load_workbook(filename = xlsx_dir)   # xlsx_dir is the workbook path

ws = wb["Details"]   # Details is the sheet name
2 Answers

You need to use the worksheets property of the workbook object

ws = wb.worksheets[0]

you need to use worksheet function sheet_by_name

sheet =  wb.sheet_by_name("Details")
Related