When reading an excel file in Python can we know which column/field is filtered

2024/11/15 18:01:36

I want to capture the field or column name that is filtered in the excel file when reading through python. I saw that we can also capture only the filtered rows by using openpyxl and using hidden == False (How to import filtered excel table into python?). In my project it is important to identify which field/column is filtered in the excel file. Is it possible? and how to achieve? Adding an example.

pip install openpyxl
from openpyxl import load_workbookwb = load_workbook('test_filter_column.xlsx')ws = wb['data'] 

![enter image description here

This is non hidden data while if the gender column is filtered below[![enter image description here][2]][2] this.

So what I am expecting is my output should be giving gender as that is filtered. If more than one field is filtered then expecting to provide all the filtered column names.

Answer

Inspired by this post, but adapted to your case, and also accounting for when there are multiple filters:


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)

Output:

{'col_id': [3], 'col_letter': ['D'], 'col_name': ['gender'], 'filter_values': [['F']]}

I think this covers the examples you gave, and hopefully shows some other info you might want too.

https://en.xdnf.cn/q/119023.html

Related Q&A

Error:__init__() missing 1 required positional argument: rec

I am new to python. I am trying to do microphone file that ought to detect, listen, record and write the .wav files. However, it is giving me an error while I am trying to run the file. It is saying:Ty…

Maya: Connect two Joint chains with Parent Constraint

So here is a snipit of an IK spine builder Ive been working on. Ive figure out how to make lists to duplicate the bound into an IK chain, what Ive got stuck on however is I want my list and for loop to…

What is the equivalent for onkeydown and onkeyup (Javascript events) in python?

There are events called onkeydown and onkeyup in Javascript. Can anyone please suggest the python equivalent of it?

Matching number string pairs

I have the following sample string:R10666: 273141 C1 + 273141 C2 + 273141 C3 + 273141 C4 + 273141 C5 - 273141 C6I want to obtain:[(273141,C1), ..., (- 273141, C6)]The numbers can be floating point numb…

Turning a text file into a tabular format [duplicate]

This question already has answers here:How do I print parameters of multiple objects in table form? [duplicate](2 answers)Line up columns of numbers (print output in table format)(7 answers)Closed 5 y…

Python: Read file with list as list

I have placed a list in a text file. I want python to read the text file and return the contents as a list. However, it is instead reading the content as a string:Text file:[a,b,c]Python:ids=[]writtenF…

Tkinter scrollbar not scrollable

I followed some tutorial on attaching a scrollbar to a textbox. However, in the tutorial, the scrollbar is really a "bar". When I tried myself, I can only press the arrows to move up or down,…

How to create multiple roles through discord.py bot?

I have been trying to make my discord bot create multiple roles through a command. But it simply doesnt work. Here is what I have done so far: @commands.command()async def create_roles(self, ctx):guild…

python: how do i know when i am on the last for cycle

for i in range(len(results_histogram)):if i!=len(results_histogram)-1:url+=str(results_histogram[i])+,my if statement is checking whether i am on the last loop, but it is not working. what am i doing w…

scrape text in python from https://brainly.co.id/tugas/148

scrape "Jawaban terverifikasi ahli" in green box from the url https://brainly.co.id/tugas/148, possibly the color of green tick icon to the left of it also(tag <use xlink:href="#icon-…