Openpyxl: How to add filters to all columns
You can simply read 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')
All you need to do is to set worksheet.auto_filter.ref
to the full range of worksheet cells.
import openpyxl
from openpyxl.utils import get_column_letter
workbook = openpyxl.load_workbook('Data/Test01.xlsx')
worksheet = workbook['Sheet1']
FullRange = "A1:" + get_column_letter(worksheet.max_column) \
+ str(worksheet.max_row)
worksheet.auto_filter.ref = FullRange
workbook.save('Data/Test03.xlsx')