利用Python,将多个excel文件合并为一个文件
思路
利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中。
完整代码
# -*- coding: utf-8 -*- #将多个Excel文件合并成一个 import xlrd import xlsxwriter #打开一个excel文件 def open_xls(file): fh=xlrd.open_workbook(file) return fh #获取excel中所有的sheet表 def getsheet(fh): return fh.sheets() #获取sheet表的行数 def getnrows(fh,sheet): table=fh.sheets()[sheet] return table.nrows #读取文件内容并返回行内容 def getFilect(file,shnum): fh=open_xls(file) table=fh.sheets()[shnum] num=table.nrows for row in range(num): rdata=table.row_values(row) datavalue.append(rdata) return datavalue #获取sheet表的个数 def getshnum(fh): x=0 sh=getsheet(fh) for sheet in sh: x+=1 return x if __name__=='__main__': #定义要合并的excel文件列表 allxls=['F:/test/excel1.xlsx','F:/test/excel2.xlsx'] #存储所有读取的结果 datavalue=[] for fl in allxls: fh=open_xls(fl) x=getshnum(fh) for shnum in range(x): print("正在读取文件:"+str(fl)+"的第"+str(shnum)+"个sheet表的内容...") rvalue=getFilect(fl,shnum) #定义最终合并后生成的新文件 endfile='F:/test/excel3.xlsx' wb1=xlsxwriter.Workbook(endfile) #创建一个sheet工作对象 ws=wb1.add_worksheet() for a in range(len(rvalue)): for b in range(len(rvalue[a])): c=rvalue[a][b] ws.write(a,b,c) wb1.close() print("文件合并完成")
源文件excel1:
源文件excel2:
运行结果:
合并后的excel3:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[FLAC/分轨][251.78MB]
- 黄韵玲2001-巴结音乐[全员集合][WAV+CUE]
- 郑绪岚2015-流金岁月·精选[首版][WAV+CUE]