【python】合并表格

本代码兼顾了合并sheet与合并表格的功能

import os
import pandas as pddef combine_sheet(excel_path, number):
    #excel_path is str and num is the sequence number of the excel
    excel = pd.read_excel(excel_path, sheet_name = None, header = None)
    keys = list(excel.keys())
    excel_contact = pd.DataFrame()
    for i in keys:
        excel_sheet = excel[i]
        excel_contact = pd.concat([excel_contact, excel_sheet])
    excel_contact.to_excel(‘vocabulary{}.xlsx‘.format(number), header= False, index=None)

def combine_excel(excels_path):
    #excel_path is a list of excel files‘ name
    excel_combination = []
    for i in excels_path:
        excel_combination.append(pd.read_excel(i, header=None))
    new_excel = pd.concat(excel_combination)
    new_excel.to_excel(‘vocabulary.xlsx‘, header = False, index=None)

os.chdir(‘C:\\Users\\11440\\OneDrive‘)
combine_sheet(‘词汇.xlsx‘, 1)
combine_sheet(‘词汇2.xlsx‘, 2)

excel_list = ["vocabulary1.xlsx", "vocabulary2.xlsx"]
combine_excel(excel_list)

相关推荐