Search code examples
python-3.xindexingopenpyxlworksheet

Get Worksheet index from title or instance using Openpyxl


How can someone get the Worksheet's index number using a Worksheet's property with Openpyxl?

The best example would be an Excel workbook that contains 3 sheets: "Aaa", "Bbb" and "Ccc". Knowing the title "Bbb", what would be the way of getting the index number of that sheet?


Solution

  • wb.worksheets.index(wb.get_sheet_by_name('Bbb')) is the right answer. Thanks to BeeR for pointing it out.

    Edit: With Openpyxl's update, the correct syntax would be

    wb.worksheets.index(wb['Bbb'])