Setting active sheet by Sheet instead of index

Issue #933 resolved
Joshua Arnott
created an issue

The following syntax seems intuitive to me:

wb = openpyxl.Workbook()
sheet = wb.create_sheet("New Sheet")
wb.active = sheet

This code doesn't raise an exception, until wb.active is accessed again:

>>> wb.active
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Miniconda3\envs\python36\lib\site-packages\openpyxl\workbook\workbook.py", li
ne 134, in active
    return self._sheets[self._active_sheet_index]
TypeError: list indices must be integers or slices, not Worksheet

Instead we're required to pass the index:

wb.active = wb.get_index(sheet)

It seems like it should be easy to make the earlier code function as expected by automatically calling get_index if a Sheet is passed.

Comments (3)

  1. CharlieC

    I agree that the current setting is counterintuitive and that it would be better to be able to pass in the sheet or sheet name. Just haven't had the time to get round to making what would essentially be an API change.

  2. Log in to comment