Setting active sheet in loaded workbook selects multiple sheets

1,768 views
Skip to first unread message

stev...@gmail.com

unread,
Oct 9, 2017, 5:46:01 PM10/9/17
to openpyxl-users
I am trying to set the active sheet before I save the workbook.  Setting workbook.active does select the correct sheet by index, but it keeps the previously active sheet selected as well.  If you create blank workbook with 3 sheets and name it blank.xlsx, this code will reproduce what I am seeing.  Any suggestions?

import openpyxl

outputFile="blank.xlsx"
workbook = openpyxl.load_workbook(outputFile)
workbook.active = 2
workbook.save(filename = outputFile)

Charlie Clark

unread,
Oct 10, 2017, 7:14:21 AM10/10/17
to openpyx...@googlegroups.com
I'm not quite sure what you mean is the problem but it sounds like you're
referring to the way sheets are grouped in workbook views. Unfortunately,
the specification is not very clear on how this is supposed to work.


Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

stev...@gmail.com

unread,
Oct 10, 2017, 4:41:26 PM10/10/17
to openpyxl-users
I think the issue is related to the worksheet view, however I am not interacting directly with it.  When a workbook is loaded from a file, the active worksheet is selected and active.  If you then use the "setter" of workbook.active and set it to a different worksheet that worksheet becomes the active sheet, however the "tabSelected" value of the original active worksheet remains "True" so now 2 tabs are selected (highlighted) when you open the workbook.  I have confirmed that the same does not happen when creating a worrkbook, only when loading an existing one.  The only solution I have found is to create a new sheet and copy the data from the original active sheet to it then delete the original active sheet.

Charlie Clark

unread,
Oct 12, 2017, 6:46:41 AM10/12/17
to openpyx...@googlegroups.com
Am .10.2017, 22:41 Uhr, schrieb <stev...@gmail.com>:

> I think the issue is related to the worksheet view, however I am not
> interacting directly with it. When a workbook is loaded from a file, the
> active worksheet is selected and active. If you then use the "setter" of
> workbook.active and set it to a different worksheet that worksheet
> becomes
> the active sheet, however the "tabSelected" value of the original active
> worksheet remains "True" so now 2 tabs are selected (highlighted) when
> you
> open the workbook. I have confirmed that the same does not happen when
> creating a worrkbook, only when loading an existing one. The only
> solution
> I have found is to create a new sheet and copy the data from the original
> active sheet to it then delete the original active sheet.

FWIW this is what I used to work around something similar in a file with
chart sheets and invisible worksheets with data:

wb.views[0].firstSheet = 1

I'd love to be able to explain this but I can't as the specification says
nothing about grouping worksheets. :-/ If anyone can come up with
something suitable I'd love to add it to the documentation!
Reply all
Reply to author
Forward
0 new messages