Python Forum

Full Version: Multiple excel files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am so new at using python that I don’t know how to start the code.

I have 26 excel files (file1, file2, file3, etc) and I want to create a for loop that will open each file one at a time and copy sheet 1 and paste it into a new excel file called template.xlsx sheet 1, and then save the template as “new file 1”. Then open file2 copy data from sheet1 and paste it into template.xlsx sheet1 and do save as “new file 2”. And continue to do this process 24 more times.

Thank you,
Kristen
A good start would be to look at a package called "Pandas".
Python + pandas makes it easy to open excel files.

Paul
You can put all the file names in a list (say names).
Next, you import pandas(install if not installed already.
import a files and merge them together.
Next convert this dataframe to excel file back.Here is an example.
names=["file1.xlsx",file2.xlsx"]
for i in files:
    df=pd.read_excel(i,sheet_name='Sheet1') 
    df=df.megre(df1) #you can modify this according to your files
df.to_excel("output.xlsx")  
This should give you an idea to solve your problem.
I feel like maybe I didn't explain what I'm trying to do very well. Sorry, super new to this.

I have 26 excel files: File1 File2 File3 File4 Etc...

And I have one template.xlsx file that has formulas in it.

I want to create a for loop that will open File1 copy all the data inside sheet1, (A1:N) N cell always varies in range.

Then open the template file and paste the copied data to sheet1 (B13:O). Then do a save as “new file 1”.

Then repeat this process 25 more times. Open file2 copy data from sheet1 (A1:N), open template paste data to sheet1 (B13:O) save as “new file 2”.

I’ve already downloaded appropriate packages.
Well, however you describe it, your best bet is to
import pandas, and the many examples available will
show you exactly how to do what you want.

paul
I have googled for day's and everything I try doesn't do what I want. I'm just looking for some help on how to write code for this. I figured a python forum would be the best place for this.
an example of a for loop to open 26 files
for number in range(1, 27):
    print(f'open file{number}')
    # code to deal with the excel file
Output:
open file1 open file2 ... ... ... open file25 open file26