import openpyxl # 打开 Excel 文件 workbook = openpyxl.load_workbook('example.xlsx') # 选择工作表 sheet = workbook['Sheet1'] # 过滤数据 sheet.auto_filter.ref = 'A1:C10' sheet.auto_filter.add_filter_column(0, ['A', 'B']) sheet.auto_filter.add_sort_condition('C2:C10') # 保存 Excel 文件 workbook.save('example.xlsx')