3

1行Python代码,合并100个Excel文件,竟然这么方便?!

 1 year ago
source link: https://blog.51cto.com/u_15493782/5429210
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1行Python代码,合并100个Excel文件,竟然这么方便?!_python

大家好,这里是程序员晚枫。

❤先说一个好消息,python-office自动化办公的官网上线了,点击直达👉 https://www.python-office.com

今天开源项目 python-office发布了一个新功能:

1行代码,合并你指定的多个Excel文件。

本文给大家详细介绍一下~

有一位老师,现在有全校1年级12个班级所有同学,一共12个成绩单Excel文件,现在老师想把它们合并到一个文件:一年级.xlsx里,每个班级作为一个单独的sheet存放。如图所示,

1行Python代码,合并100个Excel文件,竟然这么方便?!_官网_02

这里大可放心,哪怕每个表的格式、内容不同,也完全可以无损合并。这里用班级成绩合并举例,只是为了大家更好的理解。

1行代码实现

下面我们用一行代码,实现上面这个功能。

安装python-office这个库

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-office -U
# 导入这个库:python-office,简写为office
import office

#1行代码,验证是否绑定成功
office.excel.merge2excel(dir_path=r'C:\程序员晚枫\excel-merge\excel',output_file='test.xlsx')

#参数作用:
# dir_path = 文件夹的位置,建议把需要合并的多个excel文件放到同一个文件夹里。
# output_file = 最终合并的excel文件放在哪里、叫什么名字,可以不填,默认是:merge2excel.xlsx

直接运行以上代码,就可以得到一个合并后的excel文件啦~

快去试试吧~

如果有我没说清楚的,或者在使用过程中有问题,欢迎大家在评论区和我交流~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK