使用python将多个excel合并
最近看视频学习,老师布置了个作业,关于如何使用python将多个excel进行合并,老师写的代码我感觉比较复杂,下面是我自己改良之后较简单的方式。
实现这个功能主要有两种方法,一种是用xlwd,xlsxwriter库结合,不管是xlsx,xls都是适用的;另一种是openpyxl,这个库只对xlsx有效。
本文针对第一种方式进行讲解。
首先准备三个excel文件,目的是将三个excel的sheet进行合并:
test_excel1.xlsx


为了简化测试,另外两个excel文件test_excel2.xlsx, test_excel3.xlsx的内容跟第一个excel是一致的。
下面是代码部分
-- coding:utf-8 --
import xlrd,xlsxwriter
待合并excel
allxls=["E:\python3_hellobi_work\excel\test_excel\test_excel1.xlsx",
"E:\python3_hellobi_work\excel\test_excel\test_excel2.xlsx",
"E:\python3_hellobi_work\excel\test_excel\test_excel3.xlsx"]
目标excel
end_xls="E:\python3_hellobi_work\excel\test_excel\final_excel.xlsx"
def open_xls(file):
try:
fh=xlrd.open_workbook(file)
return fh
except Exception as e:
print("打开文件错误:"+e)
根据excel名以及第几个标签信息就可以得到具体标签的内容
def get_file_value(filename,sheetnum):
rvalue=[]
fh=open_xls(filename)
sheet=fh.sheets()[sheetnum]
row_num=sheet.nrows
for rownum in range(0,row_num):
rvalue.append(sheet.row_values(rownum))
return rvalue
获取第一个excel的sheet个数以及名字作为标准
first_file_fh=open_xls(allxls[0])
first_file_sheet=first_file_fh.sheets()
first_file_sheet_num=len(first_file_sheet)
sheet_name=[]
for sheetname in first_file_sheet:
sheet_name.append(sheetname.name)
定义一个目标excel
endxls=xlsxwriter.Workbook(end_xls)
all_sheet_value=[]
把所有内容都放到列表all_sheet_value中
for sheet_num in range(0,first_file_sheet_num):
all_sheet_value.append([])
for file_name in allxls:
print("正在读取"+file_name+"的第"+str(sheet_num+1)+"个标签...")
file_value=get_file_value(file_name,sheet_num)
all_sheet_value[sheet_num].append(file_value)
print(all_sheet_value)
num=-1
sheet_index=-1
将列表all_sheet_value的内容写入目标excel
for sheet in all_sheet_value:
sheet_index+=1
end_xls_sheet=endxls.add_worksheet(sheet_name[sheet_index])
num+=1
num1=-1
for sheet1 in sheet:
for sheet2 in sheet1:
num1+=1
num2=-1
for sheet3 in sheet2:
num2+=1
#print(num,num1,num2,sheet3)
#在第num1行的第num2列插入sheet3内容
end_xls_sheet.write(num1,num2,sheet3)
记得要关闭文件
endxls.close()
----------------------------------------------end--------------------------------------------------------------
运行代码,看看目标excel:


两个excel已经合并到一起了。
哈哈。写完啦~~~
使用python将多个excel合并的更多相关文章
- Python将多个excel表格合并为一个表格
Python将多个excel表格合并为一个表格 生活中经常会碰到多个excel表格汇总成一个表格的情况,比如你发放了一份表格让班级所有同学填写,而你负责将大家的结果合并成一个.诸如此类的问题有很多.除 ...
- Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- 【转发】Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- python读写word、excel、csv、json文件
http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...
- Python利用pandas处理Excel数据的应用
Python利用pandas处理Excel数据的应用 最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...
- python将两个数组合并成一个数组的两种方法的代码
内容过程中,把写内容过程中常用的内容收藏起来,下面的资料是关于python将两个数组合并成一个数组的两种方法的内容,希望能对小伙伴们有帮助. c1 = ["Red","G ...
- python使用xlrd读取excel数据时,整数变小数的解决办法
python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...
- 【python-excel】Selenium+python自动化之读取Excel数据(xlrd)
Selenium2+python自动化之读取Excel数据(xlrd) 转载地址:http://www.cnblogs.com/lingzeng86/p/6793398.html ·········· ...
随机推荐
- 本地连接 vmware服务器
在本机中装载虚拟机,安装redhat.需要调试使用redhat可以与Windows进行通讯. 分为多步,在此前提下,默认你已经安装好且可以vm 和虚拟机 1:点击虚拟机>设置>添加网络适配 ...
- A strange lift
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- Flex 基础语法(三)
2.flex-wrap 默认情况下,项目都排在一条线(又称"轴线")上.flex-wrap属性定义,如果一条轴线排不下,如何换行 属性 含义 nowrap(默认值) 不换行 wra ...
- web storage 离线存储
用来保存键值对数据,数据以属性的方式保存在storage实例对象上 可以用storage1.length来决定键值对的数量,但是无法决定数据的大小,storage1.remainingSpace可 ...
- 原生addClass 方法 添加类函数
function addClass(id,new_class){ var i,n=0; new_class=new_class.split(","); ...
- J1001.Java原生桌面及Web开发浅谈
自从Java问世以来,在服务端开发方面取得了巨大的发展.但是在桌面/Web开发方面,一直没有得到大的发展.从最初的AWT,到Swing,再到JavaFX,Java从来没有在桌面/Web解决方案中取得重 ...
- python的学习之路day1
软件:python3.pycharm开发工具 python的开始:print("hello world") 注意:python3需要加上() 1.变量是什么:在程序运行过程中它的值 ...
- javaScript额外笔记
--------------------------------------------------------Part 1javascript:脚本语言辅助开发:网页的前台开发三大块:1.HTML ...
- 主机和VMware中的Linux如实现共享文件夹
当我在网上查了几小时的挂载文件夹方法后发现,VMware中的Linux的挂载和双系统的挂载不同 最终目的就是在/mnt目录下有个hgfs的文件夹 效果图: 首先打开VMware中的Linux系统 具体 ...
- 【分享】jQuery无插件实现 鼠标拖动图片切换 功能
前言 我就想随便叨逼叨几句,爱看就看几句,不爱看就直接跳过看正文就好啦~ 这个方法是仿写页面时我自己研究出来,可能有比我更简单的方法. 但我不管,因为我没查我不知道,我就觉得我的最好啦,耶耶耶~ 效果 ...