Last active
June 12, 2024 13:52
-
-
Save rdapaz/a97efc0d06b0c99c1c5563f7ded8ac6e to your computer and use it in GitHub Desktop.
Generate Column Names in Excel Spreadsheet with python and win32com
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
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