Skip to content

Instantly share code, notes, and snippets.

@dheerosaur
Created June 14, 2017 11:40
Show Gist options
  • Select an option

  • Save dheerosaur/43b4da821b538d0c1fdcb557906b8e40 to your computer and use it in GitHub Desktop.

Select an option

Save dheerosaur/43b4da821b538d0c1fdcb557906b8e40 to your computer and use it in GitHub Desktop.
An example of processing an Excel sheet
def process_cell(cell):
if cell.is_date and cell.value:
return cell.value.strftime('%m/%d/%Y')
elif '"$"#' in cell.number_format:
if isinstance(cell.value, float) or isinstance(cell.value, int):
return '${:,.2f}'.format(cell.value)
return cell.value
def process_claims(sheet):
heads, rows = [], []
for row in sheet.iter_rows():
cells = [process_cell(cell) for cell in row[:3]]
if not any(cells):
continue
elif len(heads) < 2:
heads.append(cells[0])
else:
rows.append(cells)
return heads, rows
def claims_from_excel(source):
try:
workbook = load_workbook(source)
except InvalidFileException:
raise Exception('Problems importing Claims file')
contents = []
for worksheet in workbook.worksheets:
heads, rows = process_claims(worksheet)
contents.append({
'title': worksheet.title,
'heads': heads,
'rows': rows,
})
return contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment