python|python xlrd 封装

【python|python xlrd 封装】python xlrd 封装
参考: gist

pip install xlrd==1.2.0

#!/usr/bin/env python # -*- coding: utf-8 -*-__author__ = "leo" __time__ = "2017-11-11"import xlrdclass Excel: def __init__(self, excel_name): self.excel_name = excel_name self.sheet_name = None self.excel = None self.sheet = Nonedef get_excel(self): self.excel = xlrd.open_workbook(filename=self.excel_name) return self.exceldef get_sheet_by_name(self): excel = xlrd.open_workbook(filename=self.excel_name) self.sheet = excel.sheet_by_name(self.sheet_name) return self.sheetdef get_key_value_list(self, start=1, end=None): if end is None: self.get_sheet_by_name() l = [] row_nums = self.sheet.nrows col_nums = self.sheet.ncols names = self.sheet.row_values(0) for row in range(start, row_nums): app = {} for col in range(0, col_nums): app[names[col]] = self.sheet.cell_value(row, col) l.append(app)return lif __name__ == '__main__': e = xlrd.open_workbook('a.xlsx') s = e.sheet_by_name('b') names = s.row_values() row_nums = s.nrows col_nums = s.ncols

    推荐阅读