How do I clear a column filter with openpyxl

Viewed 416

I run into problems with existing spreadsheets where users have applied an autofilter and then filtered on some criteria before saving the workbook. I want to clear these filters preferably using openpyxl but cannot seem to figure out how to do it. Setting them is straight forward.

So what I have tried is to actually just move the autofilter to some other range. This works in moving the filter away from the affected columns, it does not clear the filter though. It remains in place.

Using the code below, I can set autofilter:

from openpyxl import load_workbook

filename = "path_to\\filter_test.xlsx"

wb = load_workbook(filename)
ws = wb.active
ws.auto_filter.ref = ws.dimensions
wb.save(filename)

I then open the spreadsheet and set the filter to some state to filter the data and save the workbook.

I then move the filter:

from openpyxl import load_workbook

filename = "C:\\Users\\paul.nel\\Desktop\\filter_test.xlsx"

wb = load_workbook(filename)
ws = wb.active
ws.auto_filter.ref = "Z100"
wb.save(filename)

Which moves it but does not unfilter the previous columns. The docs do not address this specifically and no search have been constructive.

0 Answers
Related