excel 읽기

5367 단어
#coding: utf-8
import xlrd
class Xlinfo(object):
    def __init__(self,path = ''):
        #  
        self.xl = xlrd.open_workbook(path)
    #excel float 
    def floatToStr(self, val):
        # float string , 
        if isinstance(val, float):
            val =str(int(val))
        return val
#    Excel sheet list 
    def get_sheet_info(self):
        listkey = ['uname','pwd']
        infolist = []
        #  0 
        for row in range(1, self.sheet.nrows):
            info =[self.floatToStr(val) for val in self.sheet.row_values(row)]
            tmp = zip(listkey, info)
            infolist.append(dict(tmp))
        return infolist

#      
    def get_sheetinfo_by_name(self, name):
        self.sheet = self.xl.sheet_by_name(name)
        return self.get_sheet_info()

    #  
    def get_sheetinfo_by_index(self, index):
        self.sheet = self.xl.sheet_by_index(index)
        return self.get_sheet_info()


if __name__ == '__main__':
    path = r'D:\guojunqi\user_info.xlsx'
    info = Xlinfo(path).get_sheetinfo_by_index(0)
    print info


좋은 웹페이지 즐겨찾기