2

I have 7 excel sheets in one workbook and I am trying to copy and paste the data from each excel sheet into my final sheet. the code below creates the final sheet called 'Final Sheet' but does not copy any of the data from each sheet. I need a loop to go through each sheet and copy and paste the data into the final sheet but don't know how to do it.

Sheet 1 = North America, Sheet 2 = Japan, Sheet 3 = China etc

`#create final list sheet
 open = openpyxl.load_workbook(filepath)
 ws2 = open.create_sheet('Final List') # this creates the final sheet
 open.save(filepath)`

`#put data into final list
 wb = openpyxl.load_workbook(filepath)
 sheet1 = open.get_sheet_by_name('North America')
 finalListSheet = open.get_sheet_by_name('Final List')
 wb.save(filepath)`

1 Answer 1

1

A similar question was asked here: Python Loop through Excel sheets, place into one df

I simplify this here. This method use Pandas:

import pandas as pd

sheets_dict = pd.read_excel(filepath, sheetname=None)

full_table = pd.DataFrame()

//Loop in sheets
for name, sheet in sheets_dict.items():
    sheet['sheet'] = name
    full_table = full_table.append(sheet)

//Need to save the DF in your Final Sheet

Here's another question about how to save dataframe (DF) in specific Excel sheet: Pandas Dataframe to excel sheet

Sign up to request clarification or add additional context in comments.

2 Comments

thanks - when I save the DF to the final sheet it removed all my other sheets. any help??
You should look at how to add new sheet to Excel file for that part: stackoverflow.com/questions/40385689/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.