

Save the master dataframe into an Excel spreadsheet.If yes, read the file content (data), and append/add it to the master dataframe variable called df.Loop through all the files in the current working directory, determine if a file is Excel by checking the file name ends with “.xlsx”.df = pd.DataFrame()ĭf = df.append(pd.read_excel(file), ignore_index=True) Instead of opening up Excel, data is stored inside your computer’s memory. Think about copying a block of data from one Excel file and pasting it into another. If your Excel file contains more than 1 sheet, continue reading to the next section.ĭf.append() will append/combine data from one file to another. Be aware that this method reads only the first tab/sheet of the Excel file by default. Pd.read_excel() will read Excel data into Python and store it as a pandas DataFrame object. We loop through all the files within the current working directory, but only process the Excel files whose name ends with “.xlsx”.

Next, we create an empty dataframe df for storing the data for master spreadsheet. Combine multiple Excel files into one spreadsheet Notice there are non-Excel files, and we don’t want to open those, so we’ll handle that soon. The variable cwd shows the path to the current working directory, and the variable files is a list of all the file names within the current working directory. import osįiles = os.listdir(cwd) Getting current working directory and files within it Then find the current working directory, as well as all the file names within the directory. We’ll start by importing these two libraries.

We use this library to load Excel data into Python, manipulate data, and recreate the master spreadsheet. Pandas library is the gold standard for data analysis and manipulation. We use this library to get all the Excel file names, including their paths. Such as manipulating folder and file paths. Oslibrary gives a way of using operating system dependent functionalities.
#How to merge workbooks how to
If you need help with installing Python or libraries, here’s a guide on how to do that. Although you can combine as many Excel files as you wish, we’ll use three files to demonstrate the process.
#How to merge workbooks code
If you want to follow along, feel free to grab the source code and files used in this tutorial from here. For this exercise, we’ll need to use two Python libraries: os and pandas.
