एक फ़ोल्डर में सभी एक्सेल फाइलों को मर्ज करने के लिए, ग्लोब मॉड्यूल और एपेंड () विधि का उपयोग करें।
मान लें कि डेस्कटॉप पर हमारी एक्सेल फाइलें निम्नलिखित हैं -
बिक्री1.xlsx

बिक्री2.xlsx

नोट - आपको openpyxl और xlrd संकुल को संस्थापित करने की आवश्यकता हो सकती है।
सबसे पहले, वह पथ सेट करें जहां सभी एक्सेल फाइलें जिन्हें आप मर्ज करना चाहते हैं, स्थित हैं। एक्सेल फ़ाइलें प्राप्त करें और ग्लोब का उपयोग करके उन्हें पढ़ें -
path = "C:\\Users\\amit_\\Desktop\\"
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames) इसके बाद, मर्ज किए गए आउटपुट एक्सेल फ़ाइल के लिए एक खाली डेटाफ़्रेम बनाएं जो उपरोक्त दो एक्सेल फ़ाइलों से डेटा प्राप्त करेगा -
outputxlsx = pd.DataFrame()
अब, वास्तविक प्रक्रिया को देखा जा सकता है यानी सबसे पहले, लूप के लिए एक्सेल फाइलों का उपयोग करके पुनरावृति करें। एक्सेल फ़ाइलें पढ़ें, उन्हें संक्षिप्त करें और डेटा संलग्न करें -
for file in filenames: df = pd.concat(pd.read_excel(file, sheet_name=None), ignore_index=True, sort=False) outputxlsx = outputxlsx.append(df, ignore_index=True)
उदाहरण
निम्नलिखित कोड है -
import pandas as pd
import glob
# getting excel files to be merged from the Desktop
path = "C:\\Users\\amit_\\Desktop\\"
# read all the files with extension .xlsx i.e. excel
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)
# empty data frame for the new output excel file with the merged excel files
outputxlsx = pd.DataFrame()
# for loop to iterate all excel files
for file in filenames:
# using concat for excel files
# after reading them with read_excel()
df = pd.concat(pd.read_excel( file, sheet_name=None), ignore_index=True, sort=False)
# appending data of excel files
outputxlsx = outputxlsx.append( df, ignore_index=True)
print('Final Excel sheet now generated at the same location:')
outputxlsx.to_excel("C:/Users/amit_/Desktop/Output.xlsx", index=False) आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा अर्थात, मर्ज की गई एक्सेल फ़ाइल उसी स्थान पर उत्पन्न होगी -
