1、excel文件操作
# -*- coding: utf-8 -*- import xlrd workbook = xlrd.open_workbook(‘D:\\workspace\\eclipse-python\\test\\myexcel.xls‘) sheetname=workbook.sheet_names()#抓取所有sheet页的名称 print ‘myexcel is ‘,sheetname[0],sheetname[1],sheetname[2] #定位到sheet1 worksheet1=workbook.sheet_by_name(u‘【工作月报】‘) #遍历sheet1中所有行row num_rows=worksheet1.nrows print u‘总行数=‘,num_rows for i in range(num_rows): row=worksheet1.row_values(i) print u‘获取的第‘,i,u‘行的数据是‘,row[0],row[1],row[2] #遍历sheet1中所有列col num_cols=worksheet1.ncols print u‘总列数=‘,num_cols for j in range(num_cols): col=worksheet1.col_values(j) print u‘获取的第‘,i,u‘列的数据是‘,col[0],col[1],col[2],col[3],col[4] #遍历sheet1中所有单元格cell for rown in range(num_rows): for coln in range(num_cols): cell = worksheet1.cell_value(rown,coln) print cell
时间: 2024-10-20 18:28:32