Last active
August 3, 2018 04:48
-
-
Save dnaga392/2167c0b989710753cee8dd2f1905ef9d to your computer and use it in GitHub Desktop.
(python) Get range address script for Excel
This file contains 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
import string | |
def get_range_address(row, col): | |
"""Convert to range address string | |
ex) | |
(4, 1) => 'A4' | |
(1, 52) => 'AZ1' | |
Same as follows: | |
xlwings.Range((row, col)).get_address(False, False) | |
This is fast. but this allow to get invalid address(: 'A0', 'XYZ72'). | |
""" | |
def _address_char_generator(row, col): | |
"""Return chars of address string | |
returned reversed chars | |
ex) | |
(4, 1) => ['4', 'A'] ... A4 | |
(1, 52) => ['1', 'Z', 'A'] ... AZ1 | |
""" | |
yield str(row) | |
numbers = string.ascii_uppercase | |
n = len(numbers) | |
while col > 0: | |
yield numbers[col % n - 1] | |
col = (col - 1) // n | |
chars = tuple(_address_char_generator(row, col)) | |
return ''.join(reversed(chars)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment