import os
import pandas as pd
dir = './table_dir'
# 获取目录下所有的表
origin_file_list = os.listdir(dir)
print(origin_file_list)
with pd.ExcelWriter('result.xls') as writer:
# 循环遍历表格
for i in origin_file_list:
# 拼接每个文件的路径
file_path = dir + '/' + i
# 把表名赋予给对应的sheet
sheet_name = i[:-4]
df = pd.read_excel(file_path)
#变相解决表格中第一行第一列为空的缺陷
string = "".join(list(str(i) for i in df.index))
# 判断如果索引都为数字,则不保留索引(根据自己代码调整)
if string.isdigit():
df.to_excel(writer, sheet_name,index=False)
else:
df.to_excel(writer, sheet_name)
python脚本 将多个excel表格合并成一个表格中的多个sheet
最新推荐文章于 2024-08-24 10:04:35 发布
本文介绍了一种使用Python批量处理Excel文件的方法,通过遍历指定文件夹内的所有表格,并将它们合并到一个Excel文件中,每个原始文件作为单独的工作表。特别地,该脚本能够智能处理表格索引,确保输出格式的整洁。
2万+

被折叠的 条评论
为什么被折叠?



