Skip to content

Instantly share code, notes, and snippets.

@mithunkamat
Created May 18, 2020 07:10
Show Gist options
  • Save mithunkamat/27a80ca5a276b10b39780e4225bec6af to your computer and use it in GitHub Desktop.
Save mithunkamat/27a80ca5a276b10b39780e4225bec6af to your computer and use it in GitHub Desktop.
Example code iteratively building a 'mappings' dictionary from a table in Excel using openpyxl. On seeing two blank lines, we assume the table has ended.
empty = 0 # initialize our empty cell count
while empty < 2:
# increment the row number (which begins at the row we found 'Internal'
row += 1
# assign the value we find in the 'Internal' column to internal
internal = ws[f'B{row}'].value
if internal is None:
empty += 1 # if we get blank row, increment empty counter
else:
# otherwise we have a mapping to add to the mappings dict
mappings[internal] = ws[f'D{row}'].value
empty = 0 # also re-initialize the empty counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment