简体   繁体   English

Python Openpyxl - Append 许多 excel 文件合并为 1 个文件

[英]Python Openpyxl - Append many excel files into 1 file

I have 10 Excel files (they have same number of columns, and varying number of rows)我有 10 个 Excel 文件(它们具有相同的列数和不同的行数)

I need to append data from those 10 files into one single Excel file using Openpyxl Python library我需要使用 Openpyxl Python 库将这 10 个文件中的 append 数据转换为一个 Excel 文件

Read data from File1, append it to new_file Read data from File2, append it to new_file...从File1读取数据,append到new_file 从File2读取数据,append到new_file...

Is this possible?这可能吗? Can anyone help me?谁能帮我?

Thank you谢谢

There are some missing details in the question, as raised by @moken.正如@moken 所提出的,问题中缺少一些细节。 Let's make some assumptions that all files have a single sheet named 'Sheet 1' and identical column headers.让我们假设所有文件都有一个名为“Sheet 1”的工作表和相同的列标题。 And the final output will start with file10's content, then file9 etc and we will skip copying the column headers.最后的 output 将从 file10 的内容开始,然后是 file9 等等,我们将跳过复制列标题。

For the sake of simplicity, we will use 3 files' content for illustration:为了简单起见,我们将使用3个文件的内容来说明:

file1.xlsx:文件 1.xlsx:

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

file2.xlsx:文件 2.xlsx:

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

file3.xlsx:文件 3.xlsx:

col_1 col_1 col_2列_2 col_3 col_3
F3-1 F3-1 F3-2 F3-2 F3-3 F3-3
F3-2 F3-2 F3-3 F3-3 F3-4 F3-4
F3-3 F3-3 F3-4 F3-4 F3-5 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:代码相当简单,我们从当前文件和 append 逐行获取所有行到主工作簿:

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:最终的 output 将包含具有以下值的行:

>>> 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')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM