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

Python 读取excel

2016-03-31 16:32 579 查看
#! encoding=utf-8
import xlrd,xlwt
import xml.dom.minidom
import os,sys

class OpExcel():
table = ''
tabledata = []
def __init__(self,path,sheet):
try:
data = xlrd.open_workbook(path)
except Exception, e:
print str(e)
sys.exit()
self.table = data.sheets()[sheet]

def readexcel(self,nrows=0,ncols=0,colnameindex=0):
if (nrows == 0) and (ncols == 0):
# 没有定义要读取的行数和列数就取excel中的行数和列数
nrows = self.table.nrows
ncols = self.table.ncols
# print self.table.nrows
colnames =  self.table.row_values(colnameindex)
for row in range(1,nrows):
rowdata = self.table.row_values(row)
if rowdata:
tmp = {}
for i in range(0,len(colnames)):
tmp[colnames[i]] = rowdata[i]
self.tabledata.append(tmp)
return self.tabledata
def readexcel2(self,nrows=0,ncols=0):
if (nrows == 0) and (ncols == 0):
# 没有定义要读取的行数和列数就取excel中的行数和列数
nrows = self.table.nrows
ncols = self.table.ncols
for row in range(1,nrows):
tmp = []
for col in range(0,ncols):
tmp.append(self.table.cell(row,col).value)
self.tabledata.append(tmp)
return self.tabledata
# def writeexcel(self,nrow,ncol,data):
#     self.table.cell(nrow,ncol).value = data

if __name__ == "__main__":
path = r'D:\Python\excel\2\23.xlsx'
e = OpExcel(path,0)
data = e.readexcel()
print data
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python excel