python合并excel多个sheet

#用python实现将三个excel合并成一个excel
#第一个测试文件 第二个测试文件 第三个测试文件
# 其中每个文件中有多个sheet,需要将其全部合并

import xlrd,xlsxwriter

#设置要合并的所有文件
allxls=["/Users/xubin/myapp/pythonfile/第一个测试文件.xlsx","/Users/xubin/myapp/pythonfile/第二个测试文件.xlsx","/Users/xubin/myapp/pythonfile/第三个测试文件.xlsx"]
#设置合并到的文件
endxls ="/Users/xubin/myapp/pythonfile/endxls.xlsx"

#打开表格
def open_xls(file):
    try:
        fh=xlrd.open_workbook(file)
        return fh
    except Exception as e:
        print(str("打开出错,错误为:"+e))

#获取所有sheet
def getsheet(fh):
    return fh.sheets()

#读取某个sheet的行数
def getnrows(fh,sheet):
    table=fh.sheets()[sheet]
    content=table.nrows
    return content

#读取某个文件的内容并返回所有行的值
def getfilect(fh,fl,shnum):
    fh=open_xls(fl)
    table=fh.sheet_by_name(shname[shnum])
    num=getnrows(fh,shnum)
    lenrvalue=len(rvalue)
    for row in range(0,num):
        rdata=table.row_values(row)
        rvalue.append(rdata)
    print(rvalue[lenrvalue:])
    filevalue.append(rvalue[lenrvalue:])
    return filevalue

#存储所有读取的结果
filevalue=[]
#存储一个标签的结果
svalue=[]
#存储一行结果
rvalue=[]
#存储各sheet名
shname=[]

#读取第一个待读文件,获得sheet数
fh=open_xls(allxls[0])
sh=getsheet(fh)
x=0
for sheet in sh:
    shname.append(sheet.name)
    svalue.append([])
    x+=1
#依次读取各sheet的内容
#依次读取各文件当前sheet的内容
for shnum in range(0,x):
    for fl in allxls:
        print("正在读取文件:"+str(fl)+"的第"+str(shnum)+"个标签的…")
        filevalue=getfilect(fh,fl,shnum)
    svalue[shnum].append(filevalue)
    #print(svalue[0])
    #print(svalue[1])
#由于apped具有叠加关系,分析可得所有信息均在svalue[0][0]中存储
#svalue[0][0]元素数量为sheet标签数(sn)*文件数(fn)
sn=x
fn=len(allxls)
endvalue=[]

#设置一个函数专门获取svalue里面的数据,即获取各项标签的数据
def getsvalue(k):
    for z in range(k,k+fn):
        endvalue.append(svalue[0][0][z])
    return endvalue

#打开最终写入的文件
wb1=xlsxwriter.Workbook(endxls)
#创建一个sheet工作对象
ws=wb1.add_worksheet()
polit=0
linenum=0
#依次遍历每个sheet中的数据
for s in range(0,sn*fn,fn):
    thisvalue=getsvalue(s)
    tvalue=thisvalue[polit:]
    #将一个标签的内容写入新文件中
    for a in range(0,len(tvalue)):
        for b in range(0,len(tvalue[a])):
            for c in range(0,len(tvalue[a][b])):
                #print(linenum)
                #print(c)
                data=tvalue[a][b][c]
                ws.write(linenum,c,data)
            linenum+=1
    #叠加关系,需要设置分割点
    polit=len(thisvalue)
wb1.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值