简体   繁体   中英

Combining several SHEETS into one EXCEL

I am using this code to put all Excel files and sheets into one. And it works flawlessly. But in some occasions i want to take all the excel files, and only put all the sheets from all the files into a single excel file. I know i can use Copy sheet in excel, but I want to do it to multiple documents. Keep all the data as it was, no added columns or rows, and keep the name of the sheet aswell . I am sure pandas has such a function, so I do not do it manually. If you have an idea please help. Example: Workbook1 Sheet1 Sheet2 Sheet3

Workbook2 Sheet11 Sheet22

Apply code...

FinalWorkbook Sheet1 Sheet2 Sheet3 Sheet11 Sheet22

This code puts all the data into a single sheet.

import os
import pandas as pd
print("Combine xls and xlsx")
cwd = os.path.abspath('') 
files = os.listdir(cwd)  
## get all sheets of a given file
df_total = pd.DataFrame()
for file in files:                         # loop through Excel files
    if file.endswith('.xls') or file.endswith('.xlsx'):
        excel_file = pd.ExcelFile(file)
        sheets = excel_file.sheet_names
        for sheet in sheets:               # loop through sheets inside an Excel file
            print (file, sheet)
            df = excel_file.parse(sheet_name = sheet)
            df_total = df_total.append(df)
print("Loaded, ENTER to combine:")
dali=input()
df_total.to_excel('Combined/combined_file.xlsx')
print("Done")
dali=input()

Here is the answer. I hope someone finds this useful. Combines all sheets from all excel files XLS od XLSX to a single excel file with all sheets.

import pandas as pd
import openpyxl
print("Copying sheets from multiple files to one file")
cwd = os.path.abspath('') 
files = os.listdir(cwd)  

df_total = pd.DataFrame()
df_total.to_excel('Combined/combined_file.xlsx') #create a new file
workbook=openpyxl.load_workbook('Combined/combined_file.xlsx')
ss_sheet = workbook['Sheet1']
ss_sheet.title = 'TempExcelSheetForDeleting'
workbook.save('Combined/combined_file.xlsx')


for file in files:                         # loop through Excel files
    if file.endswith('.xls') or file.endswith('.xlsx'):
        excel_file = pd.ExcelFile(file)
        sheets = excel_file.sheet_names
        for sheet in sheets:               # loop through sheets inside an Excel file
            print (file, sheet)
            df = excel_file.parse(sheet_name = sheet)
            with pd.ExcelWriter("Combined/combined_file.xlsx",mode='a') as writer:  
                df.to_excel(writer, sheet_name=f"{sheet}", index=False)
            #df.to_excel("Combined/combined_file.xlsx", sheet_name=f"{sheet}")

workbook=openpyxl.load_workbook('Combined/combined_file.xlsx')
std=workbook["TempExcelSheetForDeleting"]
workbook.remove(std)
workbook.save('Combined/combined_file.xlsx')
print("Loaded, press ENTER to end")
dali=input()
#df_total.to_excel('Combined/combined_file.xlsx')
print("Done")
dali=input()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM