当用Python读取excel文件时,我们可以知道哪些列/字段被过滤了

vij*_*iji -1 python

我想捕获通过python读取时在excel文件中过滤的字段或列名称。我发现我们还可以使用 openpyxl 并使用 hide == False 来仅捕获过滤后的行(How to importfiltered excel table into python?)。在我的项目中,确定 excel 文件中过滤了哪个字段/列非常重要。是否可以?以及如何实现?添加一个例子。

pip install openpyxl
from openpyxl import load_workbook

 wb = load_workbook('test_filter_column.xlsx')
 ws = wb['data'] 
Run Code Online (Sandbox Code Playgroud)

![在此输入图像描述

这是非隐藏数据,而如果性别列在下面被过滤[![在此处输入图像描述][2]][2] 这个。

所以我期望的是我的输出应该给出经过过滤的性别。如果过滤了多个字段,则期望提供所有过滤后的列名称。

小智 5

受到这篇文章的启发,但适应了您的情况,并且还考虑了存在多个过滤器的情况:


from openpyxl import load_workbook
from openpyxl.utils import get_column_letter

# Load workbook
wb = load_workbook('/path/to/xlsx')
# Extract sheet
ws = wb['data']

# Create a dict to store relevant info
filters = {}

# Get the ID of the columns that are filtered
filters['col_id'] = [col.col_id for col in ws.auto_filter.filterColumn]

# Get the letter of the columns that are filtered
# This appears to be one-indexed, but get_column_letter
# is zero indexed
filters['col_letter'] = [get_column_letter(col + 1) for col in filters['col_id']]

# Extract the column name - assuming that it is
# given in row 1 of the column
filters['col_name'] = [ws[f'{col}1'].value for col in filters['col_letter']]

# Get the values of the filters being used
filters['filter_values'] = [col.filters.filter for col in ws.auto_filter.filterColumn]

print(filters)

Run Code Online (Sandbox Code Playgroud)

输出:

{'col_id': [3], 'col_letter': ['D'], 'col_name': ['gender'], 'filter_values': [['F']]}
Run Code Online (Sandbox Code Playgroud)

我认为这涵盖了您给出的示例,并希望显示您可能还需要的其他一些信息。