Last active
June 23, 2021 15:24
-
-
Save adejones/e0d8783dad234ed400420e0193b692a7 to your computer and use it in GitHub Desktop.
openpyxl (2.4.8) sheet-to-dict like the csv DictReader - based on https://gist.github.com/mdellavo/853413
This file contains 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 openpyxl import load_workbook | |
def XLSXDictReader(f): | |
book = load_workbook(f) | |
sheet = book.active | |
rows = sheet.max_row | |
cols = sheet.max_column | |
headers = dict((i, sheet.cell(row=1, column=i).value) for i in range(1, cols)) | |
def item(i, j): | |
return (sheet.cell(row=1, column=j).value, sheet.cell(row=i, column=j).value) | |
return (dict(item(i, j) for j in range(1, cols + 1)) for i in range(2, rows + 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment