最近看视频学习,老师布置了个作业,关于如何使用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合并的更多相关文章

  1. Python将多个excel表格合并为一个表格

    Python将多个excel表格合并为一个表格 生活中经常会碰到多个excel表格汇总成一个表格的情况,比如你发放了一份表格让班级所有同学填写,而你负责将大家的结果合并成一个.诸如此类的问题有很多.除 ...

  2. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  3. python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter

    最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...

  4. 【转发】Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  5. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

  6. Python利用pandas处理Excel数据的应用

    Python利用pandas处理Excel数据的应用   最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...

  7. python将两个数组合并成一个数组的两种方法的代码

    内容过程中,把写内容过程中常用的内容收藏起来,下面的资料是关于python将两个数组合并成一个数组的两种方法的内容,希望能对小伙伴们有帮助. c1 = ["Red","G ...

  8. python使用xlrd读取excel数据时,整数变小数的解决办法

    python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...

  9. 【python-excel】Selenium+python自动化之读取Excel数据(xlrd)

    Selenium2+python自动化之读取Excel数据(xlrd) 转载地址:http://www.cnblogs.com/lingzeng86/p/6793398.html ·········· ...

随机推荐

  1. input输入框校验,字母,汉字,数字等

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-e ...

  2. CentOS6编译LAMP基于FPM模式的应用wordpress

    CentOS6编译LAMP基于FPM模式的应用wordpress 引言:其实我们可以直接使用yum安装LAMP(Linux+Apache[httpd]+Mysql+PHP),比手动编译安装LAMP要简 ...

  3. ubuntu中python3.4安装pip

    这两天碰到在ubuntu中安装pip的问题. 第一种方法 用百度搜索了一下,基本上都是这个命令: sudo apt-get install python3-pip 但是,用这条命令下载速度特别慢. 第 ...

  4. eclipse中project facet问题

    一般出现在从别处import的项目上,只有项目文件夹上有红叉,其他地方都正常,现总结个人的几个解决方案: 有几种可能: 1,编码设置是否一致,也即是你项目原来的编码和现在eclipse用的默认编码是否 ...

  5. linux dig 命令

    dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的用法就是查询单个主机的信息. $ dig baidu.com dig 命令默认的输出信息 ...

  6. JDBC(MySQL)一周学习总结(二)

    上一篇文章我们总结了获取数据库连接以及操作数据表的一些知识点,本篇将继续上次的文章给大家分享! 1. 上一篇文章我们可以对数据表进行增删改查的操作了,对与一些小项目的部分功能我们也足以胜任.但现在有一 ...

  7. Clipboard 剪辑板

    ie是最早支持剪辑板相关事件(并且允许javascript接入)的浏览器(鼠标右键复制)   相关事件: beforecopy— Fires just before the copy operatio ...

  8. Maven 项目pom.xml报错

    Maven项目报 Failure to transfer org.apache.maven.plugins:maven-*-plugin:pom 原因是maven的plugin并未下载到本地 或者本地 ...

  9. 使用.NET Core在RESTful API中进行路由操作

    介绍 当列出REST API的最佳实践时,Routing(路由)总是使它位于堆栈的顶部.今天,在这篇文章中,我们将使用特定于.NET Core的REST(web)API来处理路由概念. 对于新手API ...

  10. Files and Directories

    Files and Directories Introduction     In the previous chapter we coveredthe basic functions that pe ...