1

In Python, I have the code below that reads all files with sheet name 'EA':

df = pd.read_excel(file_, sheet_name='EA')

The problem is that there are slight variations in some of the files, for example some sheets are named EA 1, EA X, etc.

Is there a way to write the code to basically say 'include sheet names that have EA in their name'?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Arthur NS
  • 19
  • 2
  • Does this answer your question? [read-multiple-excel-file-with-different-sheets-names-in-pandas](https://stackoverflow.com/questions/21125596/read-multiple-excel-file-with-different-sheets-names-in-pandas) – Anurag Dabas Aug 19 '21 at 07:34
  • I should have mentionned that some of the sheets shouldn't be included in the dataframe, so we don't want all sheets, only those containing 'EA' – Arthur NS Aug 19 '21 at 07:40

1 Answers1

1
import xlrd
xls = xlrd.open_workbook(r'<path_to_your_excel_file>', on_demand=True)
sheets = xls.sheet_names()

And then, filter out the sheets you need. on_demand=True loads sheets when needed. This will be useful if it is a heavy excel with many sheets.

filtered_sheets = []
for sheet in sheets:
    if 'EA' in sheet:
        filtered_sheets.append(sheet)

# Now load each sheet in filtered_sheets using pd.read_excel()

Amith Lakkakula
  • 506
  • 3
  • 8