使用第三方库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 ‘‘