Openpyxl remove row group

Viewed 105

I need to remove row groups from an excel file using Openpyxl. What I already figured out: there is an option to create a row group, but I can't find an option to remove those. Is this not supported and if not: is there a workaround?

Thanks in advance!

2 Answers

Removing row groups is just like creating them, but you need to select level zero using "outline_level=0". Below you find a code example to remove groups in the entire sheet:

import openpyxl as xl
wb = xl.load_workbook()
ws = wb.active
ws.row_dimensions.group(1, ws.max_row + 1, outline_level=0) # for entire sheet

The general command is based around using delete_rows. Normally specifying, the active worksheet, the delete command and specifying the row range (starting row, ending row).

import openpyxl as xl
wb = xl.load_workbook()
ws = wb.active
ws.delete_rows(firstrow, numberofrows) #for multiple row deletion
ws.delete_rows(1, ws.max_row+1) # for entire sheet
ws.delete_rows(rownum) #for single row
Related