随笔 - 29  文章 - 0  评论 - 0  阅读 - 47865

python-合并多个excel表格至一个excel多个sheet中

'''
将多个excel表格合并至一个excel多个sheet
'''

复制代码
 1 import os
 2 import pandas as pd
 3 
 4 dir ='./tstdir'
 5 # 获取目录下所有的表
 6 origin_file_list = os.listdir(dir)
 7 print(origin_file_list)
 8 
 9 with pd.ExcelWriter('tables.xls') as writer:
10     # 循环遍历列表
11     for i in origin_file_list:
12         # 拼接每个文件的路径
13         file_path = dir + '/' + i
14         # 把表名赋予给对应的sheet,去掉文件类型.xlsx
15         sheet_name = i[:-5]
16         df = pd.read_excel(file_path)
17         df.to_excel(writer,sheet_name,index=False)
18         # 变相解决表格中第一行第一列为空的缺陷
19         string = "".join(list(str(i) for i in df.index))
20         # 判断如果索引都为数字,则不保留索引
21         if string.isdigit():
22              df.to_excel(writer,sheet_name,index=False)
23         else:
24              df.to_excel(writer,sheet_name)
复制代码

 

posted on   Wuxuanlin  阅读(1011)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示