0

So I have an excel file with multiple sheets called inventory, inventory1, inventory2...all the way up to inventory 10. However there are sheets other than the inventory sheets, so I can't just read all sheets of the excel file and concat. Is there a way to read all of these sheets, the inventory sheets, with pandas, without writting the sheet names individually?

Thank you

  • yes it can be done , the following link https://stackoverflow.com/questions/26521266/using-pandas-to-pd-read-excel-for-multiple-worksheets-of-the-same-workbook has an good example, – Himanshu Kandpal Jan 24 '21 at 02:12
  • Hey hkandpal, thank you for your response. Unfortunatley I carelessly forgot one important specification, there are other sheets in the excel file which don't have that name structure and I don't want to read, so I can't put filename = none and just concat. My mistake, I forgot that important detail. – moltke_colombia Jan 24 '21 at 14:16

1 Answers1

0

this may work to filter and read only tabs with "inventory*" as the tab name.

-- untested

import pandas as pd
import re

xl = pd.ExcelFile('file.xlsx')
dataframes = []
for sheet in xl.sheet_names:
    if re.match('inventory*', sheet):  # when matching pattern add the dataframe to the list
        dataframes.append(pd.read_excel('foo.xlsx', sheet_name=sheet))
Himanshu Kandpal
  • 1,261
  • 8
  • 11