-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolumn_letters.py
More file actions
28 lines (23 loc) · 820 Bytes
/
Copy pathcolumn_letters.py
File metadata and controls
28 lines (23 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def index_to_column_letters(index: int) -> str:
"""Convert a 0-based column index to Excel-style letters (A, B, ..., Z, AA, AB, ...).
Args:
index: Zero-based column index (0 => 'A').
Returns:
The Excel-style column label as a string.
Raises:
ValueError: If index is negative.
"""
if not isinstance(index, int):
raise TypeError("index must be an int")
if index < 0:
raise ValueError("index must be non-negative")
# Excel-like base-26 with no zero digit: 0->A, 25->Z, 26->AA
result = []
n = index
while True:
n, rem = divmod(n, 26)
result.append(chr(ord('A') + rem))
if n == 0:
break
n -= 1 # Carry handling due to lack of zero digit in Excel columns
return ''.join(reversed(result))