python xlrd 封装
参考: gist
pip install xlrd==1.2.0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "leo"
__time__ = "2017-11-11"
import xlrd
class Excel:
def __init__(self, excel_name):
self.excel_name = excel_name
self.sheet_name = None
self.excel = None
self.sheet = None
def get_excel(self):
self.excel = xlrd.open_workbook(filename=self.excel_name)
return self.excel
def get_sheet_by_name(self):
excel = xlrd.open_workbook(filename=self.excel_name)
self.sheet = excel.sheet_by_name(self.sheet_name)
return self.sheet
def 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 l
if __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
网友评论