用Python一键汇总千个Excel表格,做办公室最靓的仔(附程序)_python 批量汇总excel文件(1)

    # 创建一个工作簿
    book = xlwt.Workbook(encoding="utf-8")
    # 创建表单
    sheet = book.add_sheet(excel_sheet_Name)
    # 写入表头
    for i in range(0,len(excelTitle)):
        sheet.write(0, i, excelTitle[i])
    book.save(path)
    return True
except Exception as e:
    return False

1.1 总表初始化(用来解决上面的问题)

def initExcel2(destExcel_path, sourceExcel_path,total_sheet_name):
“”"

:param destExcel_path: 合并总表excel的路径
:param sourceExcel_path: 需要合并excel的路径
:param total_sheet_name: 合并总表后sheet的名字
:return: 返回False or True
"""
try:
    # 创建一个工作簿
    book = xlwt.Workbook(encoding="utf-8")
    # 创建表单,并给表单起个名字
    sheet = book.add_sheet(total_sheet_name)
    # 获取待需合并excel的所有文件
    excel_name_list = get_All_Excelname(sourceExcel_path)
    # 一个待合并execl的路径
    excel_path = sourceExcel_path + "/" + excel_name_list[0]
    # 获取excel的sheet
    excel_sheet = get_excel_sheet(excel_path)
    # 获取excel的表头数据
    excel_title_list = excel_sheet.row_values(0)
    # 写入表头
    for i in range(0,len(excel_title_list)):
        sheet.write(0, i, excel_title_list[i])
    book.save(destExcel_path)
    return True
except Exception as e:
    return False

2.获取需要合并的所有的excel文件名

def get_All_Excelname(path):
“”"

:param path: 待合并excel文件的路径
:return:
"""
excelName_list = os.listdir(path)
# print(excelName_list)
return excelName_list

返回excel表的sheet对象

def get_excel_sheet(path):
# 打开指定路径的excle表
book = xlrd.open_workbook(path)
# 获取excle中的表单
sheet = book.sheet_by_index(0)
# 返回sheet对象
return sheet

返回总表的wtbook,sheet对象

def get_total_excel_sheet(path):
“”"

:param path: 存放总表的path
:return:
"""
book = xlrd.open_workbook(path, formatting_info=True)
wtbook = xlutils.copy.copy(book)
wtsheet = wtbook.get_sheet(0)
return wtbook,wtsheet

4. 开始遍历(合并excel表)

def writeExcel(destExcel_path,source_path,excelName_list):
“”"

:param destExcel_path: 合并总表存放的路径
:param source_path: 需要合并excel的路径
:param excelName_list: 需要合并excel表的文件名称
:return:
"""
# 用来记录总表中的行数
total_excel_row = 1
# 获取总表的book,sheet
total_book,total_sheet = get_total_excel_sheet(destExcel_path)
for excelName in excelName_list:
    # 文件路径
    excelPath = source_path + excelName
    # 获取表的sheet对象
    sheet = get_excel_sheet(excelPath)
    # 获取行数
    n_rows = sheet.nrows
    # 开始遍历读取数据,并写入数据
    for row_index in range(1,n_rows):
        # 获取一行的数据,列表形式
        row_data_list = sheet.row_values(row_index)
        # 将数据写入到总表中
        for j in range(0,len(row_data_list)):
            total_sheet.write(total_excel_row,j,str(row_data_list[j]))
        # 每写一行,总表行数加1
        total_excel_row = total_excel_row + 1
total_book.save(destExcel_path)
print("数据合并已完成")
print("合并后的数据共有%d条" % (total_excel_row - 1))

创建文件夹

def makeDir(path):
“”"
:param path: 传入需要创建文件夹的路径
:return:
“”"
if not os.path.exists(path):
os.mkdir(path)

def main():
# 待需合并的excel文件夹路径
source_excel_path = “./excels/”
# 存放合并后的excel表文件夹路径
dest_dir = “./destDir”
# 创建文件夹
makeDir(dest_dir)
# 合并excel表名
total_excel_name = “总表.xls”
# 合并表存放路径
total_excel_path = dest_dir + “/” + total_excel_name
# 合并总表中的sheet的名字
total_excel_sheet_name = “汇总表”
# 初始化表
flag = initExcel2(total_excel_path,source_excel_path,total_excel_sheet_name)
if flag:
excelName_list = get_All_Excelname(“./excels”)
# 打印有多少个excel表
print(“总共有%d个excel表需要合并” %len(excelName_list))
# 写数据
writeExcel(total_excel_path,source_excel_path, excelName_list)
else:
print(“初始化表失败”)

if name == ‘main’:
main()
time.sleep(3)


**运行结果:**


![img](https://img-blog.csdnimg.cn/img_convert/06f7a41c38e3487c7dde7fbb93c05529.png)  
 ![img](https://img-blog.csdnimg.cn/img_convert/5ddde15a5080773d86f673a40f598605.png)


感谢您的支持。喜欢的点赞转发关注,感谢您一直以来的陪伴!


已打包成自动合并工具,需要的自行下载。


工具效果展示:


![img](https://img-blog.csdnimg.cn/img_convert/d5b29247ecde699018d54393b2aafaf4.png)  
 ![img](https://img-blog.csdnimg.cn/img_convert/4de34a048de7de6b700fb9445753d59c.png)  
 **最后,千万别辜负自己当时开始的一腔热血,一起变强大变优秀。**


文末有福利领取哦~
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

👉**一、Python所有方向的学习路线**

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。![img](https://img-blog.csdnimg.cn/c67c0f87cf9343879a1278dfb067f802.png)

👉**二、Python必备开发工具**

![img](https://img-blog.csdnimg.cn/757ca3f717df4825b7d90a11cad93bc7.png)  
👉**三、Python视频合集**

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。  
![img](https://img-blog.csdnimg.cn/31066dd7f1d245159f21623d9efafa68.png)

👉 **四、实战案例**

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。**(文末领读者福利)**  
![img](https://img-blog.csdnimg.cn/e78afb3dcb8e4da3bae5b6ffb9c07ec7.png)

👉**五、Python练习题**

检查学习结果。  
![img](https://img-blog.csdnimg.cn/280da06969e54cf180f4904270636b8e.png)

👉**六、面试资料**

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。  
![img](https://img-blog.csdnimg.cn/a9d7c35e6919437a988883d84dcc5e58.png)

![img](https://img-blog.csdnimg.cn/5db8141418d544d3a8e9da4805b1a3f9.png)

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值