合并同一个目录下的多张excel表

目的
将同一目录下的表格1和表格2合并到一张新表new_file.xlsx中

目录如下
image

合并操作的代码如下

点击查看代码
import pandas as pd
import os

# 文件路径
file_dir = r'D:\数字化\测试\测试数据\数据准备(正式测试使用)\数据字典整理_最新\数据字典合并_python'
# 构建新的表格名称
new_filename = file_dir + '\\new_file.xlsx'
# 找到文件路径下的所有表格名称,返回列表
file_list = os.listdir(file_dir)
new_list = []

for file in file_list:
    # 重构文件路径
    file_path = os.path.join(file_dir, file)
    # 将excel转换成DataFrame
    dataframe = pd.read_excel(file_path)
    # 保存到新列表中
    new_list.append(dataframe)

# 多个DataFrame合并为一个
df = pd.concat(new_list)
# 写入到一个新excel表中
df.to_excel(new_filename, index=False)
posted @ 2023-03-09 10:17  一只艾米果  阅读(115)  评论(0编辑  收藏  举报