安装xlrd模块

xlrd模块实现对excel文件内容读取

使用pip3直接安装: pip3 install xlrd 或者python官网下载http://pypi.python.org/pypi/xlrd压缩包安装

代码

创建datainfo.py

#author:命命

import xlrd
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
Path = os.path.split(curPath)[0]
sys.path.append(Path)

class ExcelUtil():
    def __init__(self, excelPath, sheetName):
        self.data = xlrd.open_workbook(excelPath)
        self.table = self.data.sheet_by_name(sheetName)
        # Get the first row as the key value
        self.keys = self.table.row_values(0)
        # Get the total number of rows
        self.rowNum = self.table.nrows
        # Get the total number of columns
        self.colNum = self.table.ncols

    def dict_data(self):
        if self.rowNum <= 1:
            print("总行数小于1")
        else:
            r = []
            j = 1
            for i in range(self.rowNum-1):
                s = {}
                # 从第二行取对应values值
                values = self.table.row_values(j)
                for x in range(self.colNum):
                    s[self.keys[x]] = values[x]
                r.append(s)
                j += 1
            return r

# if __name__ == "__main__":
#     filepath = r"F:\git\zhangmoumou1\selenium_python\testdata\dates.xlsx"  ---此为xlsx路径
#     sheetName = "a1_sta"    ---工作表名称
#     data = ExcelUtil(filepath, sheetName)
# print(data.dict_data())

详见github: https://github.com/zhangmoumou1/selenium_python

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://example.com/article/selenium-xlrd/