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))
def getsheet(fh):
return fh.sheets()
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=[]
shname=[]
fh=open_xls(allxls[0])
sh=getsheet(fh)
x=0
for sheet in sh:
shname.append(sheet.name)
svalue.append([])
x+=1
for shnum in range(0,x):
for fl in allxls:
print("正在读取文件:"+str(fl)+"的第"+str(shnum)+"个标签的…")
filevalue=getfilect(fh,fl,shnum)
svalue[shnum].append(filevalue)
sn=x
fn=len(allxls)
endvalue=[]
def getsvalue(k):
for z in range(k,k+fn):
endvalue.append(svalue[0][0][z])
return endvalue
wb1=xlsxwriter.Workbook(endxls)
ws=wb1.add_worksheet()
polit=0
linenum=0
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])):
data=tvalue[a][b][c]
ws.write(linenum,c,data)
linenum+=1
polit=len(thisvalue)
wb1.close()