Last active
May 26, 2019 14:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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