0

I am trying to ungroup the data in excel file.
I am not aware of row no which have been grouped.
When I click on 2 on top of file data gets ungrouped.
Is there a way I could do it using python.
I have set of 400-500 file for which data has to be ungrouped

I tried using win32com library

xl = win32com.client.Dispatch("Excel.Application")

#  password, act accordingly
 
#xl.Visible = True  # Optional: Hide Excel application window
wb = xl.Workbooks.Open(excel_file)
try:
    for sheet in wb.Worksheets:
        sheet_name = sheet.Name
        sheet.PageSetup.Zoom = False  # Disable zoom
        # visible_range = sheet.UsedRange.Visible
        # sheet.Find(What="*", ReplaceWith="", SearchOrder=xlPrevious, SearchDirection=xlEntireRow, MatchCase=False).Select()
        # sheet.Selection.Visible = visible_range  # Restore original visibility 
        # cells = sheet.getCells()
        # cells.ungroupRows(17,26)


  # Ungroup rows (if grouped)
        for outline in sheet.Outline:
          if outline.Type == 2:  # Check if it's an outline group
            outline.Ungroup()

1 Answer 1

0

If you just want to clear ALL the grouping on the sheet select the range and clear.

import win32com.client

xl = win32com.client.Dispatch("Excel.Application")

#  password, act accordingly
excel_file ='<file path>/excelfile.xlsx'

# xl.Visible = True  # Optional: Hide Excel application window
wb = xl.Workbooks.Open(excel_file)

for sheet in wb.Worksheets:
    sheet_name = sheet.Name
    sheet.PageSetup.Zoom = False  # Disable zoom

    ### Select col/rows and clear grouping
    sheet.UsedRange.ClearOutline()

wb.SaveAs('<file path>/excelfile_out.xlsx')

Not the answer you're looking for? Browse other questions tagged or ask your own question.