# _*_ coding: utf-8 _*_
"""
Time: 2021/8/16 15:24
Author: WJY(YunYiJia)
Version: V 0.1
File: excel.py
Describe: Blog link: https://blog.csdn.net/MeYungle
"""
import xlrd
class Excel:
def __init__(self, path):
self.excel = xlrd.open_workbook(path) # 打开文件
self.table = self.excel.sheets()[0] # 得到sheet页
self.nrows = self.table.nrows # 总行数
self.ncols = self.table.ncols # 总列数
def get(self, row, column):
""" 获取excel表单元格的数据
:param row: 行索引
:param column: 列索引
:return: 当前坐标的数据
"""
return self.table.row_values(row)[column]
def read(self, rule: dict):
"""
:param rule: 模板{'自定义列名':'列索引'}
:return: 数据字典生成器
"""
for row in range(1, self.nrows):
row_data = {key: self.get(row, col) for key, col in rule.items()}
yield row_data
if __name__ == '__main__':
excel = Excel('info.xls')
data_genr = excel.read({'FileID': 0, 'Name': 1})
for data in data_genr:
print(data)
# {'FileID': 'AS02566', 'Name': '曾小贤'}
# {'FileID': 'AE02567', 'Name': '刘大拿'}
Python 读取 Excel 示例
最新推荐文章于 2023-06-05 21:11:17 发布