python实现将当前目录下存在多个sheet页的多个表格合并成为一个表格,且满足以下条件:
1、依据第一个sheet页中某一个值进行条件筛选,然后合并第二个sheet页内容
2、sheet名为合并前的表格名
import pandas as pd
import os
# 获取当前文件夹下的所有 Excel 文件名
folder_path = '.' # 假设文件夹路径为当前文件夹
excel_files = [file for file in os.listdir(folder_path) if file.endswith('.xls')]
# 创建一个 Excel Writer 对象,用于写入 Excel 文件
with pd.ExcelWriter('合并表格.xls') as writer:
# 遍历每个 Excel 文件
for file in excel_files:
# 读取 Excel 文件中第一个工作表的数据
df_first_sheet = pd.read_excel(os.path.join(folder_path, file), sheet_name=0, header=None,engine='xlrd')
# 检查第三行第三列的数是否大于等于 5
if float(df_first_sheet.iloc[2, 2]) >= 5.0:
# 读取 Excel 文件中的第二个工作表数据
df_second_sheet = pd.read_excel(os.path.join(folder_path, file), sheet_name=1,engine='xlrd')
# 将第二个工作表的数据写入 Excel 文件,工作表的名称为当前 Excel 文件的名称(不包括扩展名)
df_second_sheet.to_excel(writer, sheet_name=os.path.splitext(file)[0], index=False)
# 打印提示信息
print("表格合并完成!")