Skip to content

Instantly share code, notes, and snippets.

@h4ndzdatm0ld
Created February 14, 2021 06:05
Show Gist options
  • Save h4ndzdatm0ld/3646135143a2140f1c8632a69bac800e to your computer and use it in GitHub Desktop.
Save h4ndzdatm0ld/3646135143a2140f1c8632a69bac800e to your computer and use it in GitHub Desktop.
Load a workbook as a dictionary
def simple_excel(workbook, sheetname, keep_vba=False, data_only=True):
wb_obj = load_workbook(
filename=workbook, keep_vba=keep_vba, data_only=data_only, read_only=True
)
wsheet = wb_obj[sheetname]
data_key = []
for value in wsheet.iter_rows(values_only=True):
for key in value:
try:
key_strip = key.strip()
data_key.append(key_strip)
except AttributeError as e:
print(f"AttributeError on key: {key}, {e}")
data_key.append(key)
break
rows = []
for rows_list in wsheet.iter_rows(values_only=True, min_row=2):
row = []
for values in rows_list:
row.append(values)
results = dict(zip(data_key, row))
rows.append(results)
wb_obj.close()
return rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment