Script not list excel search result for me

39 Views Asked by At

I wrote a python script that must search for Mar Jun Sep Dec in my xlsx file and provide name of search result columns in text file to me.

import pandas as pd

# Path to the Excel file
excel_file = r'E:\Desktop\Big_comp_cap\New Microsoft Excel Worksheet.xlsx'

# Read the Excel file
df = pd.read_excel(excel_file)

# Specify the month names to search for
months_to_search = ['Mar', 'Jun', 'Sep', 'Dec']

# Initialize a set to store matching column names
matching_columns = set()

# Iterate over each cell in the DataFrame
for column in df.columns:
    for index, value in df[column].items():
        # Check if the value is a string and contains any of the specified month names
        if isinstance(value, str):
            for month in months_to_search:
                if month in value:
                    matching_columns.add(column)
                    break  # Once a match is found, move to the next cell

# Write the column names to a text file
output_file = 'matching_columns.txt'
with open(output_file, 'w') as file:
    for col_name in matching_columns:
        file.write(col_name + '\n')

print("Matching column names have been saved to:", output_file)

but it not find any result!
note that script must look in values and not in comments or formulas
where is my script problem?

0

There are 0 best solutions below