Skip to content

Instantly share code, notes, and snippets.

@adejones
Last active June 23, 2021 15:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adejones/e0d8783dad234ed400420e0193b692a7 to your computer and use it in GitHub Desktop.
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
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