python读取Excel指定单元格的值

使用第三方库openpyxl实现

import openpyxl


def read_cell(io, sheet, cell=‘A2‘):
    """
    读取单元格
    :param io: Excel文件路径
    :param sheet: 读取哪一张表,str, int   eg: ‘Sheet1‘ or 0
    :param cell: 读取哪一个单元格
    :return: value --> str
    """
    excel = openpyxl.load_workbook(io)
    if isinstance(sheet, str):
        ws = excel[sheet]
    elif isinstance(sheet, int):
        sheet_names = excel.sheetnames
        name = sheet_names[sheet]
        ws = excel[name]
    else:
        raise TypeError(‘sheet must be int or str, not %s‘ % type(sheet))
    val = ws[cell].value
    if val:
        return val
    else:
        return ‘‘

python读取Excel指定单元格的值

上一篇:【C语言】第2章 算法 — 程序的灵魂


下一篇:org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only