1

How do I convert an integer into an alphabet or string that corresponds to Excel column alphabet? I would like to use python to do the conversion. Here are some examples;

1 -> A
10 -> J
26 -> Z
27 -> AA
30 -> AD

I am using python v3, Excel 2016 and xlwings python library to read/write to Excel.

user1315789
  • 3,069
  • 6
  • 18
  • 41
  • 1
    Which excel library/module are you using? Chances are it has a function for that. – Aran-Fey Nov 08 '17 at 11:54
  • I would like to use python to do the conversion, not Excel or VBA. – user1315789 Nov 08 '17 at 11:55
  • 1
    I'm asking about the _python_ module you're using to read/write the excel spreadsheet. – Aran-Fey Nov 08 '17 at 11:56
  • @Rawing, Oh I see. Thanks for clarifying. I am using xlwings. I am open to using any python library that provides convenient functions for the conversion. – user1315789 Nov 08 '17 at 11:56
  • 1
    I couldn't find such a feature in the xlwings library (in an admittedly cursory google search), but there are module-independent solutions in the linked duplicate. – Aran-Fey Nov 08 '17 at 12:02
  • @Rawing, the question you provided is great. It seems xlsxwriter does have such a function. Thanks! Let me test and verify first . – user1315789 Nov 08 '17 at 12:04

1 Answers1

6

Thanks to the question provided by Rawing, here is my favored solution.

import xlsxwriter

column_number = 26
x = xlsxwriter.utility.xl_col_to_name(column_number-1)
print(x)
user1315789
  • 3,069
  • 6
  • 18
  • 41
  • Instead of posting an answer, please mark your question as a duplicate. You can add your answer to the duplicate question if you want. It's better for SO if we have all answers in a single place. – Aran-Fey Nov 08 '17 at 13:48
  • Ok. I will mark it as duplicate question. However, there are many answers in that question. My answer selects the favored solution with minor changes. – user1315789 Nov 09 '17 at 13:57