简体   繁体   中英

How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python

Need some help on merging excel file with the same sheet name from multiple workbooks and saving it in a new workbook.

import pandas as pd
import glob
import os

path = "C:/Users/"
fname = glob.glob(path + "\*.xlsx")
print(fname)

mfile = pd.DataFrame()
result_DFs = []

for i in fname:
    
    df = pd.read_excel(i,sheet_name = None)
    
    for sheet in df:

        if (sheet == "Owner and Status each sheet"):
            result = result_DFs.append(df[sheet])

After this I'm stuck. Can someone help me?

Below code will append the data in result_DFs from tabname "Owner and Status each sheet" from all workbooks present in the given path;

import pandas as pd
import glob

path = r"C:\Users"
fname = glob.glob(path + "\*.xlsx")

result_DFs = pd.DataFrame()
for i in fname:  
    try:
        df = pd.read_excel(i,sheet_name = "Owner and Status each sheet")
                result_DFs = pd.concat([result_DFs,df])
    except:
        pass

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