How to get a similar sheet name in pandas

Viewed 263

I am trying to find a similar sheet name in an excel using pandas.

Currently I am using below code to get dataframe of a sheet in pandas.

excel= pd.ExcelFile(excel)
tab_name = 'Employee'
emp_df= excel.parse(tab_name)

But this code will fail if the sheet name in excel contains any space or some other extra characters.

Is there any easy way to do this ?

1 Answers

I used similarity api (fuzzywuzzy) to find similar sheet only when sheet not found error thrown when running excel.parse(tab_name)

from fuzzywuzzy import fuzz
import xlrd

try:
    tab_df = excel.parse(tab_name)
except xlrd.biffh.XLRDError:
    sheet_names=excel.sheet_names
    ratios = [fuzz.ratio(tab_name, tbname) for tbname in sheet_names]
    if(max(ratios)>50):
        tab_name = sheet_names[ratios.index(max(ratios))]
        tab_df = excel.parse(tab_name)
    else:
        logger.error(tab_name+"Not found")
Related