def _import_phenolist_xlsx(filepath, has_header):
import openpyxl
try:
wb = openpyxl.load_workbook(filepath)
assert len(wb.worksheets) == 1
sheet = wb.worksheets[0]
rows = [[cell.value for cell in row] for row in sheet.rows]
num_cols = len(rows[0])
if has_header:
fieldnames, rows = rows[0], rows[1:]
if any(fieldname is None or fieldname == '' for fieldname in fieldnames):
if has_header == 'augment':
fieldnames = [i if fieldname is None else fieldname for i, fieldname in enumerate(fieldnames)]
else:
raise PheWebError('bad xlsx header')
assert len(set(fieldnames)) == len(fieldnames), fieldnames
else:
fieldnames = list(range(num_cols))
return [{fieldnames[i]: row[i] for i in range(num_cols)} for row in rows]
except openpyxl.utils.exceptions.InvalidFileException:
if filepath.endswith('.xlsx'):
raise PheWebError("The filepath {!r} ends with '.xlsx' but reading it as an excel file failed.".format(filepath))
return None
评论列表
文章目录