Skip to content

Instantly share code, notes, and snippets.

@urodoz
Last active May 26, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urodoz/b2717e8a3af6c6f8ec2699e84167166e to your computer and use it in GitHub Desktop.
Save urodoz/b2717e8a3af6c6f8ec2699e84167166e to your computer and use it in GitHub Desktop.
Convert Excel file to list of dicts with headers as key name for each value with pyopenxl
from xlrd import open_workbook
class ExcelParser(object):
def excel_to_list(self, file_path):
wb = open_workbook(file_path)
rows = []
for sheet in wb.sheets():
number_of_rows = sheet.nrows
number_of_cols = sheet.ncols
# detecting last column
headers = []
for i in range(0, number_of_cols):
headers.append(sheet.cell(0, i).value)
for row in range(1, number_of_rows):
iteration = {}
for i in range(0, number_of_cols):
try:
value_extracted = sheet.cell(row, i).value
iteration[headers[i]] = value_extracted
except:
pass
rows.append(iteration)
return rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment