python 合并多个excel(基于python 3.X)

# 下面这些变量需要您根据自己的具体情况选择
title = ["编号","项目负责人","项目","报销事由","报销项内容*","费用合计","预约报销日期","报销人","结算信息"]
# 在哪里搜索多个表格
# filelocation = "E:\\totally\\FinancePDF"
filelocation = "E:\\totally\\FinancePDF"
# 当前文件夹下搜索的文件名后缀
fileform = ".pdf.xlsx"
# 将合并后的表格存放到的位置
filedestination = "E:\\totally\\"
# 合并后的表格命名为file
file = "Finance_All"

# 首先查找默认文件夹下有多少文档需要整合
import glob
import os
from numpy import *

filearray = []
address_Excel="E:\\totally\\FinancePDF"
f_list = os.listdir(address_Excel)
for fileNAME in f_list:
# os.path.splitext():分离文件名与扩展名
if os.path.splitext(fileNAME)[1] == '.xlsx':
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("Sheet")
except:
print("在文件%s中没有找到sheet,读取文件数据失败" % 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(title)):
if title[i][-1] == "*":
crs = 1
sheet .write_merge(0,0,i,crs+i, title[i])
# sheet.write(0, i, title[i][-2])
elif i>=4:
merge_leng=i+1
sheet.write(0, merge_leng, title[i])
else:
sheet.write(0, i, title[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")

转载于:https://www.cnblogs.com/setname/p/8417759.html

你可能感兴趣的:(python 合并多个excel(基于python 3.X))