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-06-12 15:45:50 发布