import pandas as pd
import os
# 创建一个空的数据框列表
data_frames = []
# 获取当前脚本的目录路径
current_directory = os.getcwd()
folder_path = os.path.join(current_directory, 'excel') # 相对路径:excel文件夹所在的位置
# 遍历文件夹中的所有Excel文件
for file_name in os.listdir(folder_path):
if file_name.endswith('.xlsx') or file_name.endswith('.xls'): # 只处理Excel文件
file_path = os.path.join(folder_path, file_name)
df = pd.read_excel(file_path, dtype=str) # 读取Excel文件数据
data_frames.append(df) # 将数据框添加到列表中
# 合并所有数据框
combined_data = pd.concat(data_frames, ignore_index=True)
# 获取输出文件的完整路径
output_directory = os.path.join(current_directory, 'renad')
output_path = os.path.join(output_directory, '合并后的文件路径.xlsx') # 相对路径:合并后的文件路径及文件名
# 确保输出文件夹存在
os.makedirs(output_directory, exist_ok=True)
# 保存合并后的数据框到新的Excel文件
combined_data.to_excel(output_path, index=False)
print('ok')