Skip to content

Instantly share code, notes, and snippets.

@gregorykremler
Last active January 24, 2017 23:07
Show Gist options
  • Save gregorykremler/5e68ef578e5bb9f30992 to your computer and use it in GitHub Desktop.
Save gregorykremler/5e68ef578e5bb9f30992 to your computer and use it in GitHub Desktop.
xlrd module sample use
import xlrd
# initialize a workbook
workbook = xlrd.open_workbook('<excel filename>')
# inspect a workbook's sheets
workbook.sheet_names() # returns a list of sheet names
# initialize a worksheet
worksheet = workbook.sheet_by_name('<sheet name>')
# handy to reference by index
worksheet = workbook.sheet_by_name(workbook.sheet_names()[<sheet index>])
# get worksheet number of rows
worksheet.nrows
# iterate over worksheet rows
for n in xrange(worksheet.nrows):
print worksheet.row(n)
# handle dates
def auto_date(workbook, sheet, row, row_index):
for col_index in range(len(row)):
cell = sheet.cell(row_index, col_index)
if cell.ctype == xlrd.XL_CELL_DATE:
dt = datetime.datetime(
*xlrd.xldate_as_tuple(cell.value, worksheet.book.datemode))
# dates and datetimes must be timezone "aware"
row[col_index] = tz.localize(dt, is_dst=False)
return row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment