python 项目中xlrd模块读取数据样例

1.读取数据整理成列表,可以兼容表格里日期,字符串等字符,详细请看代码:

    def get(self, sheetname,data_count): #接收两个值,第一个是表单名称,第二个是从第几行还是读
        workbook = xlrd.open_workbook(self.file) #文件名以及路径,如果路径或者文件名有中文给前面加一个r拜师原生字符。
        sheet = workbook.sheet_by_name(sheetname)  # 通过名称获取,读取第一个接收的表格
        nrows = sheet.nrows  # 获取该sheet中的有效行数
        first_row_values = sheet.row_values(1)  # 去读表格第一行数据,如果是字典模式可以当做key使用,列表可用户那单元格数据判断使用
        list_all = []
        num = data_count  #从表格第几行开始读取
        for row_num in range(1, nrows): # 循环根据拿到的行数循环多少次,拿完为止
            if row_num ==2:
                continue
            row_values = sheet.row_values(row_num)  #返回由该行中所有单元格的数据组成的列表
            if row_values:
                str_obj = []
            for i in range(len(first_row_values)): #循环取单元格里的值
                ctype = sheet.cell(num, i).ctype       #返回单元格对象
                cell = sheet.cell_value(num, i)        #返回单元格中的数据
                if ctype == 2 and cell % 1 == 0.0:     # ctype为2且为浮点
                    cell = int(cell)                   # 浮点转成整型
                    cell = str(cell)                   # 转成整型后再转成字符串,如果想要整型就去掉该行
                elif ctype == 3:
                    date = datetime(*xldate_as_tuple(cell, 0))
                    cell = date.strftime('%Y/%m/%d %H:%M:%S')
                elif ctype == 4:
                    cell = True if cell == 1 else False
                str_obj.append(cell)
            list_all.append(str_obj)
            num = num + 1
        return list_all

  

2.读取数据整理成字典,(num=1,读取第一行作为字典key使用,索引从0开始计算)可以兼容表格里日期,字符串等字符,详细请看代码:

    def get(self, sheetname): #接收两个值,第一个是表单名称,第二个是从第几行还是读
        workbook = xlrd.open_workbook(self.file) #文件名以及路径,如果路径或者文件名有中文给前面加一个r拜师原生字符。
        sheet = workbook.sheet_by_name(sheetname)  # 通过名称获取,读取第一个接收的表格
        nrows = sheet.nrows  # 获取该sheet中的有效行数
        first_row_values = sheet.row_values(1)  # 去读表格第一行数据,如果是字典模式可以当做key使用,列表可用户那单元格数据判断使用
        list_all = []
        num = 1  #读取的第一行用作字典的key使用
        for row_num in range(1, nrows): # 循环根据拿到的行数循环多少次,拿完为止
            if row_num ==2:
                continue
            row_values = sheet.row_values(row_num)  #返回由该行中所有单元格的数据组成的列表
            if row_values:
                str_obj = {}
            for i in range(len(first_row_values)): #循环取单元格里的值
                ctype = sheet.cell(num, i).ctype       #返回单元格对象
                cell = sheet.cell_value(num, i)        #返回单元格中的数据
                if ctype == 2 and cell % 1 == 0.0:     # ctype为2且为浮点
                    cell = int(cell)                   # 浮点转成整型
                    cell = str(cell)                   # 转成整型后再转成字符串,如果想要整型就去掉该行
                elif ctype == 3:
                    date = datetime(*xldate_as_tuple(cell, 0))
                    cell = date.strftime('%Y/%m/%d %H:%M:%S')
                elif ctype == 4:
                    cell = True if cell == 1 else False
                str_obj[first_row_values[i]] = cell  
            list_all.append(str_obj)
            num = num + 1
        return list_all

 

3.读取给定列数,如读取该表中第3列~5列

    def read_ncols(self, sheetname, ncols, n=1, num=1000):  # i,sheet索引
        ExcelFile = xlrd.open_workbook(self.file)
        table = ExcelFile.sheet_by_name(sheetname)
        nrows = table.nrows  # 行数
        ncols = table.ncols  # 列数
        j = 0  # 循环次数
        for row in range(1, nrows):
            j += 1
            line = []
            if self.tag == 'True':
                for col in range(0, ncols):
                    line.append(table.cell(row, col).value)
                yield line
            elif self.tag == 'False':
                if j >= n and j < n + num:
                    for col in range(0, ncols):
                        line.append(table.cell(row, col).value)
                    yield line

4.完成版示例:

import xlrd
from datetime import datetime
from xlrd import xldate_as_tuple


class ReadExcle(object):
    '''
    classdoc
    '''
    def __init__(self, file, tag='True'):
        self.file = file
        self.tag = tag

    '''
        读取页面元素表
        自动循环读取行数和列数(示例:['云A1Y6X6', '黄', '昆明驹马物流有限公司', '厢式货车', 4050.0, 740.0, 530111088325.0])
    '''

    def get(self, sheetname):
        workbook = xlrd.open_workbook(self.file)
        sheet = workbook.sheet_by_name(sheetname)  # 读取第一个sheet
        nrows = sheet.nrows  # 行数
        first_row_values = sheet.row_values(0)  # 第一行数据
        list_all = []
        num = 1
        for row_num in range(1, nrows):
            row_values = sheet.row_values(row_num)
            if row_values:
                # str_obj = {}
                str_obj = []
            for i in range(len(first_row_values)):
                ctype = sheet.cell(num, i).ctype
                cell = sheet.cell_value(num, i)
                if ctype == 2 and cell % 1 == 0.0:  # ctype为2且为浮点
                    cell = int(cell)  # 浮点转成整型
                    cell = str(cell)  # 转成整型后再转成字符串,如果想要整型就去掉该行
                elif ctype == 3:
                    date = datetime(*xldate_as_tuple(cell, 0))
                    cell = date.strftime('%Y/%m/%d %H:%M:%S')
                elif ctype == 4:
                    cell = True if cell == 1 else False
                # str_obj[first_row_values[i]] = cell  # 使用表格第一行列名为key,循环一下列值{'企业名称': '深圳市同城货的运输有限公司', '许可证编号': '440300057971', '从业资格信息证号': '452702198803112000'}
                str_obj.append(cell)  # ['深圳市同城货的运输有限公司', '440300057971', '452702198803112000']
            list_all.append(str_obj)
            num = num + 1
        return list_all

    # def get(self, sheetname):
    #     ExcelFile = xlrd.open_workbook(self.file)
    #     sheet = ExcelFile.sheet_by_name(sheetname)  # 'Sheet1'
    #     nrows = sheet.nrows  # 总行数
    #     ncols = sheet.ncols  # 列数
    #     list_all = []  # 元素名称列表
    #     for i in range(1, nrows):  # i为行数
    #         list = []
    #         for ii in range(0, ncols):
    #
    #             list.append(sheet.row(i)[ii].value)
    #         list_all.append(list)
    #     print(list_all)
    #     return list_all



    '''
        读取给定列数,如读取该表中第3列~5列
    '''

    def read_ncols(self, sheetname, ncols, n=1, num=1000):  # i,sheet索引
        ExcelFile = xlrd.open_workbook(self.file)
        table = ExcelFile.sheet_by_name(sheetname)
        nrows = table.nrows  # 行数
        ncols = table.ncols  # 列数
        j = 0  # 循环次数
        for row in range(1, nrows):
            j += 1
            line = []
            if self.tag == 'True':
                for col in range(0, ncols):
                    line.append(table.cell(row, col).value)
                yield line
            elif self.tag == 'False':
                if j >= n and j < n + num:
                    for col in range(0, ncols):
                        line.append(table.cell(row, col).value)
                    yield line

  

原文地址:https://www.cnblogs.com/iamjianghao/p/10764473.html