Created
March 3, 2011 20:06
-
-
Save mdellavo/853413 to your computer and use it in GitHub Desktop.
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
try: | |
import openpyxl | |
def XLSXDictReader(f): | |
book = openpyxl.reader.excel.load_workbook(f) | |
sheet = book.get_active_sheet() | |
rows = sheet.get_highest_row() | |
cols = sheet.get_highest_column() | |
headers = dict( (i, sheet.cell(row=0, column=i).value) \ | |
for i in range(cols) ) | |
def item(i, j): | |
return (sheet.cell(row=0, column=j).value, | |
sheet.cell(row=i, column=j).value) | |
return (dict(item(i,j) for j in range(cols)) for i in range(1, rows)) | |
except ImportError: | |
XLSXDictReader = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@erinkeith @adejones thank you both for continuing to improve on this! (and @erinkeith I'm sorry I didn't see the notification when you commented).
I'm glad to take a few lines of code out with the switch to
.max_row
and.max_column
. The current openpyxl documentation says thatget_active_sheet()
is deprecated and should be replaced with just.active
, which is working for me. I appreciate the simplicity & conciseness of @adejones's version, but for the files I work with I often run into problems because of empty cells being returned asNone
rather than the empty strings that Python's built-in CSV support treats them as. And I sometimes need to be able to specify which worksheet to use, so here's my latest version in case it helps anyone:Tested with
openpyxl==2.6.1
only.