操作excel (写入 和 读取)

一、写入excel

1 import xlwt #安装xlwt 2 book = xlwt.Workbook() 3 sheet = book.add_sheet('bai') 4 sheet.write(0,0,'姓名')#一个0 是行 ,第二个0是列 5 sheet.write(0,1,'成绩') 6 book.save('stu.xls')


  二、读取excel

 1 import xlrd  #安装xlrd
 2 
 3 book = xlrd.open_workbook('stu.xls')
 4 
 5 sheet = book.sheet_by_index(0) #读取指定的sheet页
 6 
 7 print(sheet.row_values(0))#某一行的数据
 8 print(sheet.col_values(0))#某一列的数据
 9 print(sheet.cell(0,0))#某个单元格的数据
10 print(sheet.cell(1,1))#某个单元格的数据
11 print(sheet.nrows)#总共有多少行
12 print(sheet.ncols)#总共有多少列
13 
14 for row in range(sheet.nrows):
15     print(sheet.row_values(row))
16 
17 for col in range(sheet.ncols):
18     print(sheet.col_values(col))
原文地址:https://www.cnblogs.com/baiby/p/10833751.html