Chapter1:xlrd读取Excel数据

459 阅读2分钟

1 读取Excel数据

本章讲解Python xlrd读取Excel数据。处理Excel数据从读取Excel数据开始,xlrd读取Excel数据分为3个部分:

  • 读取Sheet
  • 读取某个Sheet的行数和列数
  • 读取某个Sheet的行数据和列数据

2 环境安装

xlrd不同版本对Excel格式处理有差异: 1.2.0兼容xlsx,之后无法读取xlsx文件 如需处理xlsx格式的Excel,请安装1.2.0版本。

pip install xlrd==1.2.0

install-xlrd.png

3 Usage

使用xlrd模块,分别从三个方面读取Excel数据,讲解如下。

3.1 Excel表格

excel.png

3.2 读取Sheet

  • Test
"""
Xlrd读取Excel数据

@author xindaqi
@date 2021-06-18 15:23
"""
import xlrd
from common.utils.DataProcessUtil import DataProcessUtil
from common.constant.DigitalConstant import DigitalConstant


class XlrdReadExcel(object):
    """读取Excel数据"""

    def __init__(self, path):
        self.workbook = xlrd.open_workbook(path)

    def get_sheets(self):
        """获取Excel sheet
        参数:
            file_path: 文件路径
        返回:
            sheet_name:sheet名称
        """
        sheet_name = self.workbook.sheet_names()
        return sheet_name

if __name__ == "__main__":
    file_path = "../file/test.xlsx"
    read_excel = XlrdReadExcel(file_path)
    sheet_names = read_excel.get_sheets()
    DataProcessUtil.splitLineGenerator(DigitalConstant.TEN)
    print("Sheet name:{}".format(sheet_names))
  • 结果

sheetname.png

3.3 读取某个Sheet行数和列数

  • Test
"""
Xlrd读取Excel数据

@author xindaqi
@date 2021-06-18 15:23
"""
import xlrd
from common.utils.DataProcessUtil import DataProcessUtil
from common.constant.DigitalConstant import DigitalConstant


class XlrdReadExcel(object):
    """读取Excel数据"""

    def __init__(self, path):
        self.workbook = xlrd.open_workbook(path)

    def get_row_column_count(self, sheet_number):
        """获取Excel行数和列数
        参数:
            file_path: 文件路径
        返回:
            row_count:行数
            column_count: 列数
        """
        sheet = self.workbook.sheets()[sheet_number]
        row_count = sheet.nrows
        column_count = sheet.ncols
        return row_count, column_count

if __name__ == "__main__":
    file_path = "../file/test.xlsx"
    read_excel = XlrdReadExcel(file_path)
    row_counts, column_counts = read_excel.get_row_column_count(0)
    DataProcessUtil.splitLineGenerator(DigitalConstant.TEN)
    print("行数:{},列数:{}".format(row_counts, column_counts))
  • 结果

row-column-count.png

3.4 读取某个Sheet某行数据

  • Test
"""
Xlrd读取Excel数据

@author xindaqi
@date 2021-06-18 15:23
"""
import xlrd
from common.utils.DataProcessUtil import DataProcessUtil
from common.constant.DigitalConstant import DigitalConstant


class XlrdReadExcel(object):
    """读取Excel数据"""

    def __init__(self, path):
        self.workbook = xlrd.open_workbook(path)

    def get_sheet_row_data(self, sheet_number, row_number):
        """获取某个sheet某一行数据
        参数:
            sheet_number: sheet编号(序号)
            row_number: 行编号(序号)
        返回:
            row_data:行数据
        """
        sheet = self.workbook.sheets()[sheet_number]
        row_data = sheet.row_values(row_number)
        return row_data

if __name__ == "__main__":
    file_path = "../file/test.xlsx"
    read_excel = XlrdReadExcel(file_path)
    excel_row_data = read_excel.get_sheet_row_data(0, 0)
    DataProcessUtil.splitLineGenerator(DigitalConstant.TEN)
    print("行数据:{}".format(excel_row_data))
  • 结果

row-data.png

3.5 读取某个Sheet某列数据

  • Test
"""
Xlrd读取Excel数据

@author xindaqi
@date 2021-06-18 15:23
"""
import xlrd
from common.utils.DataProcessUtil import DataProcessUtil
from common.constant.DigitalConstant import DigitalConstant


class XlrdReadExcel(object):
    """读取Excel数据"""

    def __init__(self, path):
        self.workbook = xlrd.open_workbook(path)

    def get_sheet_column_data(self, sheet_number, column_number):
        """获取某个sheet某一列数据
        参数:
            sheet_number: sheet编号(序号)
            column_number: 列编号(序号)
        返回:
            row_data:列数据
        """
        sheet = self.workbook.sheets()[sheet_number]
        column_data = sheet.col_values(column_number)
        return column_data


if __name__ == "__main__":
    file_path = "../file/test.xlsx"
    read_excel = XlrdReadExcel(file_path)
    excel_column_data = read_excel.get_sheet_column_data(0, 0)
    DataProcessUtil.splitLineGenerator(DigitalConstant.TEN)
    print("列数据:{}".format(excel_column_data))
  • 结果

column-data.png