pyhton合并多个Excel文件
import os
import pandas as pd
import time
LOAD_DIR = "E://合成"
def read_csv_or_xlsx(file):
"""
利用pandas 读取csv或者xlsx
:return: DataFrame pandas特有数据结构 ,数据框
"""
if file.endswith(".csv"):
sheet_name = file[:-4]
data_frame = pd.read_csv(os.path.join(LOAD_DIR, file))
else:
sheet_name = file[:-5]
data_frame = pd.read_excel(os.path.join(LOAD_DIR), file)
return data_frame, sheet_name
def main():
output_file = "E://合成"
writer = pd.ExcelWriter(output_file, engine='openpyxl')
files = os.listdir(LOAD_DIR)
files.sort()
for file in files:
if os.path.isfile(os.path.join(LOAD_DIR, file)):
if file.endswith(".csv") or file.endswith(".xlsx"):
item_start = time.time()
data_frame, sheet_name = read_csv_or_xlsx(file)
data_frame.to_excel(excel_writer=writer, sheet_name=sheet_name, index=None)
writer.save()
writer.close()
if __name__ == '__main__':
main()