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
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- python学习之|python学习之 实现QQ自动发送消息
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则