Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openpyxl: How to get multiple auto-filters on a single sheet

How can I get multiple filters on a single sheet?

A single filter is easy:

 worksheet.auto_filter.ref = "A1:D4"

I can't figure out how to do multiple filters. I tried a few things like:

worksheet.auto_filter.ref = "A1:D4,A6:D9"
worksheet.auto_filter.ref = "A1:D4;A6:D9"

but noting works.

Thanks, Ryan

like image 623
Ryan Lee Avatar asked Oct 30 '22 16:10

Ryan Lee


2 Answers

The specification says that there may be only a single auto-filter per worksheet.

As long as you're happy with working with a development version you might want to look at the 2.4 development branch which has much improved filtering and sort support. I don't use filters much myself so it could use some testing and the feedback would be very helpful.

The API has changed a bit and the best documentation is in the tests:

    af = AutoFilter('A1:F1')
    af.add_filter_column(5, ["0"], blank=True)
    ws.auto_filter = af
like image 177
Charlie Clark Avatar answered Nov 21 '22 13:11

Charlie Clark


Found out I can use tables to implement the sort/filter functionality. And you can have multiple tables in a single sheet. Unfortunately I can't find a way to do this in openpyxl but XlsxWriter can do this very easily.

worksheet1.add_table('A1:C4')
worksheet1.add_table('F8:H15')
like image 25
Ryan Lee Avatar answered Nov 21 '22 13:11

Ryan Lee