I have a routine in place to convert my multiple excel files, with multiple tabs and multiple columns (some tabs are present in the excel sheets, some are not, but the column structuring inside all the tabs is the same for all the sheets) to a dictionary of dictionaries. I'm facing an issue while skipping one specific tab from some of the excel sheets. I know we define the name of the sheets which we want to include in the data structure in the sheet_name parameter in the read_excel function of pandas. But, the problem here is that I want to skip one specific tab (Sheet1) from all the excel sheets, and also, the tab names I'm defining other than that in the sheet_name parameter are not present in each of the excel sheets. Please let me know if there are any workarounds here. Thank you!!
#Assigning the path to the folder variable
folder = r'specified_path'
#Changing the directory to the database directory
os.chdir(folder)
#Getting the list of files from the assigned path
files = os.listdir(folder)
#Joining the list of files to the assigned path
for archivedlist in files:
local_path = os.path.join(folder, archivedlist)
print("Joined Path: ", local_path)
#Reading the data from the files in the dictionary data structure
main_dict = {}
def readdataframe(files):
df_dict = {}
for element in files:
df_dict[element] = pd.read_excel(element, sheet_name = ["Sheet2", "Sheet3", "Sheet4",
"Sheet5", "Sheet6", "Sheet7",
"Sheet8"])
print(df_dict[element].keys)
return df_dict
print(readdataframe(files))
I want to skip sheet1 from all the excel files wherever it is present and want to extract the sheets[2-8] from all the excel files if they are present there. Also, a side note is that I could extract all the data from all the excel files when I was using sheet_name = None, but that is not the expected result.
Lastly, all the tabs which are extracted from all the excel sheets should be a pandas data frame.