python openpyxl 筛选功能
时间: 2023-10-20 15:06:05 浏览: 101
openpyxl.pdf
5星 · 资源好评率100%
Python的openpyxl模块可以通过设置筛选条件来实现筛选功能。在使用openpyxl进行筛选时,你可以使用worksheet.auto_filter属性来设置筛选的区域,并使用worksheet.auto_filter.add_filter_column()方法来添加筛选列并指定筛选条件。例如,你可以使用以下代码来进行筛选:
```python
from openpyxl import load_workbook
workbook = load_workbook('sort_file.xlsx')
worksheet = workbook['Sheet1']
worksheet.auto_filter.ref = "A1:B12"
worksheet.auto_filter.add_filter_column(0, ["Kiwi", "Apple", "Mango"])
workbook.save("sort_file.xlsx")
```
上述代码会在A1到B12的区域内添加筛选,并在第一列中筛选出"Kiwi"、"Apple"和"Mango"。然后,你可以保存工作簿,以便在筛选后的结果中查看所需的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [最详细的Excel模块Openpyxl教程(四)-过滤和排序操作](https://blog.csdn.net/th1522856954/article/details/117340360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文