python excel简单使用 xlrd xlwt模块

import xlrd,xlwt
#读取
rbook=xlrd.open_workbook('stu.xls')#打开文件
sheet=rbook.sheet_by_index(0)#打开对应的表
nrow=sheet.nrows;ncol=sheet.ncols#找到行列总数
sheet.put_cell(0,ncol,1,'总分',None)#增加新的单元格内容,但不会在原文件更改
for row in range(1,nrow):
    t=sum(sheet.row_values(row,1))#第一个参数是行数,第二个参数是从第几个元素开始
    sheet.put_cell(row,ncol,xlrd.XL_CELL_NUMBER,t,None)
#写入
wbook=xlwt.Workbook()
wsheet=wbook.add_sheet(sheet.name)#设置新建表格名字
style=xlwt.easyxf('align:vertical center,horizontal center')#设置样式
for row in range(sheet.nrows):
    for col in range(sheet.ncols):
        wsheet.write(row,col,sheet.cell_value(row,col),style)#在新表写入内容
wbook.save('output.xls')

结果:

 

posted @ 2020-05-11 13:44  光之继承人  阅读(287)  评论(0编辑  收藏  举报