Created
June 14, 2017 11:40
-
-
Save dheerosaur/43b4da821b538d0c1fdcb557906b8e40 to your computer and use it in GitHub Desktop.
An example of processing an Excel sheet
This file contains hidden or 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
| 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