Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created October 21, 2010 18:59
Show Gist options
  • Save mdellavo/639082 to your computer and use it in GitHub Desktop.
Save mdellavo/639082 to your computer and use it in GitHub Desktop.
XLS to Dict Reader using xlrd
try:
import xlrd
def XLSDictReader(f, sheet_index=0):
data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
book = xlrd.open_workbook(file_contents=data)
sheet = book.sheet_by_index(sheet_index)
def item(i, j):
return (sheet.cell_value(0,j), sheet.cell_value(i,j))
return ( dict(item(i,j) for j in range(sheet.ncols)) \
for i in range(1, sheet.nrows) )
except ImportError:
XLSDictReader = None
@dnk8n
Copy link

dnk8n commented Sep 28, 2021

I did similar for xlsx using openpyxl. Potentially useful if you are looking here - https://gist.github.com/dnk8n/925db332579a539f9dea37301906fd45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment