您的位置:首页 > 编程语言 > Python开发

使用python读取excel合并成新的excel

2018-03-15 16:39 211 查看
import xlrd
import xlwt
data1=xlrd.open_workbook('test123.xls')
data2= xlrd.open_workbook('test.xls')
def mix(data1,data2):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True)
table1 = data1.sheets()[0]
table2 = data2.sheets()[0]
nrows1 = table1.nrows
ncols1 = table1.ncols
nrows2 = table2.nrows
ncols2 = table2.ncols
for i in range(0,nrows1+nrows2):
for j in range(0,ncols1):
if i<nrows1:
sheet1.write(i,j,table1.cell_value(i,j))
else:
sheet1.write(i,j,table2.cell_value(i-nrows1,j))
f.save('haha.xls')
data=xlrd.open_workbook('haha.xls')
return data
data=mix(data1,data2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: