Merge multiple excel files into one sheet using openpyxl and os

        This chapter introduces the application of openpyxl and os—merging multiple excel files into one sheet. There are 4 files that need to be merged under my folder, and the template of each file is the same (premise).

        It's very simple, you just need to add some code and make a little modification on the basis of the previous article, and traverse each file. And traversing every file, anyone who has read my os article should know that in the same folder, you only need to use os.listdir(). Step by step, if you are in a hurry, you can slide directly to the back to see all the codes.

import openpyxl,os

os.chdir(r"D:\临时\python试验\openpyxl\多文件合并")      # 设置默认路径。只有设置了默认路径,下方才可以直接写文件f,否则要加上路径
for f in os.listdir():
    file = openpyxl.load_workbook(f)        # 读取每个文件
    sheets = file.worksheets      # 获取该文件的所有表

    for sheet in sheets:
        for row in sheet.iter_rows(2,sheet.max_row,1,sheet.max_column):
            row_list = [i.value for i in row]
            row_list = [f.split('.')[0]] + row_list        # 把xxx区加在最前面,以方便查看合并后是数据是哪个区
            print(row_list)

        Successfully traverse each row of all tables in all files, and then write these rows into a new file. The final code is:

import openpyxl,os

new_file = openpyxl.Workbook()        # 新建一个文件用于存放合并后的数据
st = new_file.create_sheet('合并后')
st.append(['班级','姓名','语文','数学','英语','综合科'])   # 把表头加上

os.chdir(r"D:\临时\python试验\openpyxl\多文件合并")      # 设置默认路径。只有设置了默认路径,下方才可以直接写文件f,否则要加上路径
for f in os.listdir():
    file = openpyxl.load_workbook(f)        # 读取每个文件
    sheets = file.worksheets      # 获取该文件的所有表

    for sheet in sheets:
        for row in sheet.iter_rows(2,sheet.max_row,1,sheet.max_column):
            row_list = [i.value for i in row]
            row_list = [f.split('.')[0]] + row_list        # 把xxx区加在最前面,以方便查看合并后是数据是哪个区
            st.append(row_list)        # 把行写入st表里

new_file.remove(new_file['Sheet'])          # 删除创建new_file文件时默认产生的Sheet表
new_file.save(r"D:\临时\python试验\openpyxl\多文件合并.xlsx")

        All summaries are complete. It can be said that the essence of openpyxl is to iterate iter_rows, and the library's operations on data basically need to be iterated and then rewritten.

        The premise of the summary table is that the templates are the same, and then according to the style of the table, select the area to be summarized for iterative rewriting. If the templates are different, all the rows can be aggregated into one table again, which can be achieved, but what is the meaning? For example, if a column is a name, but a column in a table is a student number, it is meaningless to aggregate them into the same column in the end.

        The excel file in this article: Link: https://pan.baidu.com/s/1UH5fpdTVNggGj-8pa5cGsQ?pwd=pold 
Extraction code: pold

Guess you like

Origin blog.csdn.net/m0_71559726/article/details/130370651