Spreadsheet Column to Index Conversion in Python

If you’ve ever had to dig down into specific columns of a spreadsheet with > Z columns you probably know how annoying it is to try to convert something like ‘AQ’ to the appropriate index (e.g. row[42]). Here’s a quick one-liner to convert a spreadsheet letter-based column to the equivalent zero-based index.

For example:

  • col2num(‘A’) -> 0

  • col2num(‘Z’) -> 25

  • col2num(‘BC’) -> 54