Ungroup rows or columns

443 views
Skip to first unread message

Arth Shah

unread,
Jun 21, 2022, 6:30:32 PM6/21/22
to openpyxl-users
Hi,

I am successfully using the following command to group a set of rows and columns in an excel sheet in my python code and it's been very useful for visualization. 

 wb.column_dimensions.group(start='C', end='D', hidden=True)
new.row_dimensions.group(4, 9, hidden=True)

Would anyone know how to reverse this operation? Everytime I update an excel sheet through my code, I'd like to first delete all the existing groups and then proceed with writing information and creating new groups.  

Thanks in advance!
Arth

Charlie Clark

unread,
Jun 22, 2022, 3:07:38 AM6/22/22
to openpyxl-users

Grouping is a mess – though this is really down to the OOXML specification which conflates grouping for outline purposes with that for formatting purposes.

If you want to remove the groups then simplest thing is to delete them from the dimensions.

cds = ws.column_dimensions.keys()
rds = ws.row_dimensions.keys()

for cd in cds:
	del ws.column_dimensions[cd]

for rd in rds:
    del ws.row_dimensions[rd]

At the moment, there is no "ungroup()" method, though maybe you could submit a PR for one? I never work with groups myself, so I'm not best suited to do this.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Arth Shah

unread,
Jun 23, 2022, 11:38:31 AM6/23/22
to openpyxl-users
Thanks Charlie for your reply, this works great for my usecase :)
Message has been deleted

Sergey Krylov

unread,
Mar 17, 2025, 5:08:49 AMMar 17
to openpyxl-users
for row in ws.row_dimensions:
    if row > ws.max_row:
       ws.row_dimensions[row].outlineLevel = 0

четверг, 23 июня 2022 г. в 18:38:31 UTC+3, Arth Shah:
Reply all
Reply to author
Forward
0 new messages