Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Last active June 12, 2024 13:52
Show Gist options
  • Save rdapaz/a97efc0d06b0c99c1c5563f7ded8ac6e to your computer and use it in GitHub Desktop.
Save rdapaz/a97efc0d06b0c99c1c5563f7ded8ac6e to your computer and use it in GitHub Desktop.
Generate Column Names in Excel Spreadsheet with python and win32com
class Excel:
def __init__(self):
self.xlApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
self.xlApp.Visible = True
self.wk = self.xlApp.Workbooks.Add()
def column_name(self, iVal):
if iVal <= 0:
return ""
elif iVal <= 26:
return chr(64 + iVal)
elif iVal >= 16384:
return 'XFD'
else:
m = (iVal - 1) // 26
n = (iVal - 1) % 26 + 1
return self.column_name(m) + chr(64 + n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment