Search code examples
pythonopenpyxlxlsxpython-3.9

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


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

Solution

  • You need to use the worksheets property of the workbook object

    ws = wb.worksheets[0]