Openpyxl: How to add filters to all columns

azazelspeaks picture azazelspeaks · Jul 28, 2018 · Viewed 7.1k times · Source

I can open a worksheet, how do I add the little filter menus to all columns without turning on any filters?

I can do it in xlsxwriter with

worksheet.autofilter(0, 0, 0, num_of_col)

How do I do it in openpyxl?

Answer

Robert Hadsell picture Robert Hadsell · Mar 12, 2019

This post helped me answer my question, but instead of creating the variable for "FullRange", you can simply call ws.dimensions and it will return a string value with your range from "A1:XX". I used this to apply filters to my entire excel spreadsheet.

import openpyxl as px

wb= px.load_workbook('Data/Test01.xlsx')
ws = wb.active

ws.auto_filter.ref = ws.dimensions

wb.save('Data/Test03.xlsx')