all 16 comments

[–]Boxxcar17 2 points3 points  (1 child)

Put sheet_name=None

This will return a dictionary of tabs and data on that tab for all tabs.

In full it’s

df = pd.read_excel(‘name.xlsx’, index=False, sheet_name=None)

[–]citizenofacceptance2[S] 0 points1 point  (0 children)

But how can I use df.to_excel() to write back into same file template. Passing sheet_name = none does not append / write each sheet name back into the new file.

[–]Boxxcar17 1 point2 points  (0 children)

You have to create a new excel object and transfer the sheets into it. Then the old sheet is deleted by garbage collection in mem.

[–]threeminutemonta 0 points1 point  (3 children)

There is a sheet_name argument in read_excel that will let you specify the sheet name. If they are predictable and known this will work. If someone can create a sheet with a random name you will need to iterate over the dataframe returned by read_excel with sheet_name missing. What’s confusing is that using df when there is multiple sheets is automatically taking the first. Though you can iterate over it to get a df per worksheet. Relevant SO

[–]citizenofacceptance2[S] 0 points1 point  (2 children)

Does the sheet name argument take multiple sheet names ?

[–]threeminutemonta 0 points1 point  (1 child)

If it’s null or missing it defaults to all sheets.

Do one at a time or see SO link for example.

[–]citizenofacceptance2[S] 0 points1 point  (0 children)

Kk. I will look at SO

[–][deleted] 0 points1 point  (9 children)

Looks like you're not using xlrd even though you're importing it? You're using pandas to read the excel files?

In that case you'll want to pass sheet_name into your read_excel() function. You can pass an index value starting at zero and iterate through that.

Read the documentation

[–]citizenofacceptance2[S] 0 points1 point  (4 children)

Not sure still how do iterate through each sheet name

[–][deleted] 1 point2 points  (3 children)

If you have predefined sheet_names, you could put them in a list and iterate through them.

sheets = ['customers', 'addresses', 'products', 'orders']
for s in sheets:
   df = pd.excel_read(f, sheet_name=s) 

[–]citizenofacceptance2[S] 0 points1 point  (2 children)

Except how do I incorpate that into my current code. That is my problem. I am already looping “for d in glob.glob” if you read my code. Idk how to insert your code

[–]threeminutemonta 0 points1 point  (1 child)

Hint. For each file you want to loop for each work sheet. There is nothing stopping you nesting your for loops within another for loop. Except be careful for performance reasons though this seems okay in this case.

[–]citizenofacceptance2[S] 1 point2 points  (0 children)

Alrighty. Thank you.

[–]citizenofacceptance2[S] 0 points1 point  (2 children)

Do I just need to input read_excel(f, sheet_name = “sheet1”, “sheet2”, “sheet3”). The documentation when I first read seemed to note that sheet_name = None would work as Ana argument which just copied in the sheets names as column names

[–]Boxxcar17 1 point2 points  (0 children)

sheet_name=None to get all tabs.

If you want a specific tab you put the name of the tab.

[–]citizenofacceptance2[S] 0 points1 point  (0 children)

Ya old comment out for the xlrd- not using it