美文网首页
openpyxl3.0官方文档(30)—— 使用过滤器和排序

openpyxl3.0官方文档(30)—— 使用过滤器和排序

作者: Sinchard | 来源:发表于2020-07-15 00:12 被阅读0次

可以在工作表中添加过滤器。
注意
过滤器和排序只能由openpyxl配置,但需要在Excel等应用程序中应用。这是因为它们实际上会对范围内的单元格或行进行重排或格式化。
要添加筛选器,需要先定义范围,然后添加列和排序条件:

    from openpyxl import Workbook
    
    wb = Workbook()
    ws = wb.active
    
    data = [
        ["Fruit", "Quantity"],
        ["Kiwi", 3],
        ["Grape", 15],
        ["Apple", 3],
        ["Peach", 3],
        ["Pomegranate", 3],
        ["Pear", 3],
        ["Tangerine", 3],
        ["Blueberry", 3],
        ["Mango", 3],
        ["Watermelon", 3],
        ["Blackberry", 3],
        ["Orange", 3],
        ["Raspberry", 3],
        ["Banana", 3]
    ]
    
    for r in data:
        ws.append(r)
    
    ws.auto_filter.ref = "A1:B15"
    ws.auto_filter.add_filter_column(0, ["Kiwi", "Apple", "Mango"])
    ws.auto_filter.add_sort_condition("B2:B15")
    
    wb.save("filtered.xlsx")
    

这会将相关指令添加到文件中,但不会实际过滤或排序

相关文章

网友评论

      本文标题:openpyxl3.0官方文档(30)—— 使用过滤器和排序

      本文链接:https://www.haomeiwen.com/subject/uchchktx.html