栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python合并多个excel文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python合并多个excel文件

python合并多个Excel的代码如下:

#下面这些变量需要您根据自己的具体情况选择
biaotou=['学号','学生姓名','第一志愿','第二志愿','第三志愿','第四志愿','第五志愿','联系电话','性别','备注']
#在哪里搜索多个表格
filelocation="C:UsersannDocumentsPython Scripts"
#当前文件夹下搜索的文件名后缀
fileform="xls"
#将合并后的表格存放到的位置
filedestination="C:UsersannDocumentsPython Scripts"
#合并后的表格命名为file
file="test"
 
#首先查找默认文件夹下有多少文档需要整合
import glob
from numpy import *
filearray=[]
for filename in glob.glob(filelocation+"*."+fileform):
    filearray.append(filename)
#以上是从pythonscripts文件夹下读取所有excel表格,并将所有的名字存储到列表filearray
print("在默认文件夹下有%d个文档哦"%len(filearray))
ge=len(filearray)
matrix = [None]*ge
#实现读写数据
 
#下面是将所有文件读数据到三维列表cell[][][]中(不包含表头)
import xlrd
for i in range(ge):
    fname=filearray[i]
    bk=xlrd.open_workbook(fname)
    try:
        sh=bk.sheet_by_name("Sheet1")
    except:
        print ("在文件%s中没有找到sheet1,读取文件数据失败,要不你换换表格的名字?" %fname)
    nrows=sh.nrows 
    matrix[i] = [0]*(nrows-1)
    
    ncols=sh.ncols
    for m in range(nrows-1):  
        matrix[i][m] = ["0"]*ncols
 
    for j in range(1,nrows):
        for k in range(0,ncols):
            matrix[i][j-1][k]=sh.cell(j,k).value
#下面是写数据到新的表格test.xls中哦
import xlwt
filename=xlwt.Workbook()
sheet=filename.add_sheet("hel")
#下面是把表头写上
for i in range(0,len(biaotou)):
    sheet.write(0,i,biaotou[i])
#求和前面的文件一共写了多少行
zh=1
for i in range(ge):
    for j in range(len(matrix[i])):
        for k in range(len(matrix[i][j])):
            sheet.write(zh,k,matrix[i][j][k])
        zh=zh+1
print("我已经将%d个文件合并成1个文件,并命名为%s.xls.快打开看看正确不?"%(ge,file))
filename.save(filedestination+file+".xls")

示例:

在文件夹下有如下7个表格

作为样例,每个表格的内容均为

运行程序合并后表格如下:

更多Python知识请关注Python视频教程栏目。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/817118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号