Python 读取excel

来源:互联网 发布:简单编程代码 编辑:程序博客网 时间:2024/04/30 21:29
#! encoding=utf-8import xlrd,xlwtimport xml.dom.minidomimport os,sysclass 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 = dataif __name__ == "__main__":    path = r'D:\Python\excel\2\23.xlsx'    e = OpExcel(path,0)    data = e.readexcel()    print data
0 0
原创粉丝点击