python读取Excel整列或整行数据

单元格拆分

def get_index(capital):
    """
    大写字母(Excel列头)转数字
    :param capital: 'A' --> 0, 'AA' --> 26
    :return: int
    """
    number = 0
    capital = capital.upper()
    for char in capital:
        number = number * 26 + ord(char) - ord('A') + 1
    return number - 1

需注意xlrd的版本,只支持xls(version > 2.x),既支持xls又支持xlsx(version < 2.x)

读取整列

import xlrd


def read_col(io, sheet, cell='A1'):
    """
    读取列
    :param io: Excel文件路径
    :param sheet: 读取哪一张表,str, int   eg: 'Sheet1' or 0
    :param cell: 从哪一个单元格开始读取
    :return: value --> list
    """
    wb = xlrd.open_workbook(io)
    if isinstance(sheet, str):
        ws = wb.sheet_by_name(sheet)
    elif isinstance(sheet, int):
        ws = wb.sheet_by_index(sheet)
    else:
        raise TypeError('sheet must be int or str, not %s' % type(sheet))
    pos_col, pos_row = re.findall('[A-Z]+|[0-9]+', cell)
    col_index = get_index(pos_col)
    value = ws.col_values(col_index, start_rowx=int(pos_row) - 1)
    if value:
        return value
    return ''

读取整行

def read_row(io, sheet, cell='A1'):
    """
    读取行
    :param io: Excel文件路径
    :param sheet: 读取哪一张表,str, int   eg: 'Sheet1' or 0
    :param cell: 从哪一个单元格开始读取
    :return: value --> list
    """
    wb = xlrd.open_workbook(io)
    if isinstance(sheet, str):
        ws = wb.sheet_by_name(sheet)
    elif isinstance(sheet, int):
        ws = wb.sheet_by_index(sheet)
    else:
        raise TypeError('sheet must be int or str, not %s' % type(sheet))
    pos_col, pos_row = re.findall('[A-Z]+|[0-9]+', cell)
    col_index = get_index(pos_col)
    value = ws.row_values(col_index, start_colx=int(pos_row) - 1)
    if value:
        return value
    return ''
posted @ 2022-08-03 09:49  Rong_Z  阅读(1840)  评论(0编辑  收藏  举报