0

I have 10 Excel files (they have same number of columns, and varying number of rows)

I need to append data from those 10 files into one single Excel file using Openpyxl Python library

Read data from File1, append it to new_file Read data from File2, append it to new_file ...

Is this possible? Can anyone help me?

Thank you

2
  • 1
    Certainly possible. You state 10 files so that is 10 Excel workbooks (wb) where you want to copy the rows from a sheet in the 1st wb to Sheet1 in a new wb, then copy the rows from a sheet in the 2nd wb and also append to Sheet1 in the new wb so this data is added immediately below the data from 1st wb? Then same with 3rd wb and so on. What about headers? Do these exist (in each wb), are they the same for each of the 10, if so then presume you'd only want these copied once from the first wb? Any formatting on the data you wish to preserve? Commented Nov 30, 2022 at 2:26
  • Thank you. There are no headers, just raw data that I need to combine into one Excel file. They are all on Sheet1, and I need to put them on the Sheet1 of a new document. Commented Nov 30, 2022 at 14:28

1 Answer 1

1

There are some missing details in the question, as raised by @moken. Let's make some assumptions that all files have a single sheet named 'Sheet 1' and identical column headers. And the final output will start with file10's content, then file9 etc and we will skip copying the column headers.

For the sake of simplicity, we will use 3 files' content for illustration:

file1.xlsx:

col_1 col_2 col_3
F1-1 F1-2 F1-3

file2.xlsx:

col_1 col_2 col_3
F2-1 F2-2 F2-3
F2-2 F2-3 F2-4

file3.xlsx:

col_1 col_2 col_3
F3-1 F3-2 F3-3
F3-2 F3-3 F3-4
F3-3 F3-4 F3-5

The code is rather straightforward, where we get all rows from the current file and append row by row to the main workbook:

from openpyxl import load_workbook
main_workbook = load_workbook(filename="file3.xlsx")
file_list = ["file2.xlsx","file1.xlsx"]

for file in file_list:
    workbook = load_workbook(filename=file)
    new_rows = list(workbook['Sheet1'].values)
    for idx,row in enumerate(new_rows):
        # skip column header
        if idx == 0: continue
        main_workbook['Sheet1'].append(row)
    workbook.close()
main_workbook.save("merged.xlsx")

The final output would have rows with the following values:

>>> list(main_workbook['Sheet1'].values)
[('col_1', 'col_2', 'col_3'),
 ('F3-1', 'F3-2', 'F3-3'),
 ('F3-2', 'F3-3', 'F3-4'),
 ('F3-3', 'F3-4', 'F3-5'),
 ('F2-1', 'F2-2', 'F2-3'),
 ('F2-2', 'F2-3', 'F2-4'),
 ('F1-1', 'F1-2', 'F1-3')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.