美文网首页
python xlrd 封装

python xlrd 封装

作者: Leo_23 | 来源:发表于2021-05-09 14:14 被阅读0次

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

相关文章

  • python xlrd 封装

    python xlrd 封装参考: gist[https://gist.github.com/leo4938521...

  • python操作excel

    需要安装xlrd、pip install xlrd 操作excel 对上述代码进行封装

  • python学习

    xlrd模块 一、安装xlrd模块 到python官网下载http://pypi.python.org/pypi/...

  • xlrd模块应用

    一、安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安...

  • openpyxl读写excel

    前面介绍了利用Python中xlrd与xlwt读写Excel的基本操作( 《使用Python xlrd与xlwt模...

  • python xlrd读取和操作excel的常用方法

    安装xlrd模块(cmd命令) 引入xlrd模块和读取本地excel 常用方法 参考:python里面的xlrd模...

  • Python读取Excel

    python读取excel excel示例 1 pip安装xlrd工具 pip install xlrd 代码示...

  • python之解析execl表格

    xlrd模块 python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写...

  • 2018-09-07

    用python读写excel(xlrd、xlwt) 一、读excel表 读excel要用到xlrd模块,官网安装(...

  • excel 处理

    下载 xlrd 模块,解压xlrd.tar.gz至指定文件夹python setup.py install 完成安...

网友评论

      本文标题:python xlrd 封装

      本文链接:https://www.haomeiwen.com/subject/shgmdltx.html