{"id":26137,"date":"2017-10-26T20:03:53","date_gmt":"2017-10-26T14:33:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26137"},"modified":"2017-10-26T20:03:53","modified_gmt":"2017-10-26T14:33:53","slug":"python-programming-excel-column-name","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-excel-column-name\/","title":{"rendered":"Python Programming &#8211; Excel column name"},"content":{"rendered":"<p>MS Excel columns has a pattern like A, B, C, \u2026 ,Z, AA, AB, AC,\u2026. ,AZ, BA, BB, \u2026 ZZ, AAA, AAB \u2026.. etc. In other words, column 1 is named as \u201cA\u201d, column 2 as \u201cB\u201d, column 27 as \u201cAA\u201d.<span id=\"more-128620\"><\/span><\/p>\n<p>Given a column number, find its corresponding Excel column name. Following are more examples.<\/p>\n<pre>Input          Output\r\n 26             Z\r\n 51             AY\r\n 52             AZ\r\n 80             CB\r\n 676            YZ\r\n 702            ZZ\r\n 705            AAC<\/pre>\n<p>Suppose we have a number n, let\u2019s say 28. so corresponding to it we need to print the column name. We need to take remainder with 26.<\/p>\n<p>If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put \u2018Z\u2019 in the output string and new n becomes n\/26 -1 because here we are considering 26 to be \u2018Z\u2019 while in actual it\u2019s 25th with respect to \u2018A\u2019.<\/p>\n<p>Similarly if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do n = n\/26.<\/p>\n<p>Finally we reverse the string and print.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Example:<\/strong><br \/>\nn = 700<\/p>\n<p>Remainder (n%26) is 24. So we put \u2018X\u2019 in output string and n becomes n\/26 which is 26.<\/p>\n<p>Remainder (26%26) is 0. So we put \u2018Z\u2019 in output string and n becomes n\/26 -1 which is 0.<\/p>\n<p>Following is python implementation of above approach.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Program<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to find Excel column name from a <br\/># given column number<br\/> <br\/>MAX = 50<br\/> <br\/># Function to print Excel column name for a given column number<br\/>def printString(n):<br\/> <br\/>    # To store result (Excel column name)<br\/>    string = [&quot;\\0&quot;]*MAX<br\/> <br\/>    # To store current index in str which is result<br\/>    i = 0<br\/> <br\/>    while n &gt; 0:<br\/>        # Find remainder<br\/>        rem = n%26<br\/> <br\/>        # if remainder is 0, then a &#039;Z&#039; must be there in output<br\/>        if rem == 0:<br\/>            string[i] = &#039;Z&#039;<br\/>            i += 1<br\/>            n = (n\/26)-1<br\/>        else:<br\/>            string[i] = chr((rem-1) + ord(&#039;A&#039;))<br\/>            i += 1<br\/>            n = n\/26<br\/>    string[i] = &#039;\\0&#039;<br\/> <br\/>    # Reverse the string and print result<br\/>    string = string[::-1]<br\/>    print &quot;&quot;.join(string)<br\/> <br\/># Driver program to test the above Function<br\/>printString(26)<br\/>printString(51)<br\/>printString(52)<br\/>printString(80)<br\/>printString(676)<br\/>printString(702)<br\/>printString(705)<\/code><\/pre> <\/div>\n<p><strong>Output<\/strong><\/p>\n<pre>Z\r\nAY\r\nAZ\r\nCB\r\nYZ\r\nZZ\r\nAAC<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Python Programming &#8211; Excel column name &#8211; Mathematical Algorithms &#8211; MS Excel columns has a pattern like A, B, C, \u2026 ,Z, AA, AB, AC,\u2026. ,AZ, BA, BB, \u2026 ZZ, AAA<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,74058,4148],"tags":[77308,77342,77340,77329,77299,77341,77278,77310,77305,77309,77337,77324,77335,77307,77346,77351,77312,77320,77315,77321,77344,77327,77350,77334,77333,77322,77345,77348,77343,77306,77347,77330,77323,77349,77316,77332,77317,77339,77319,77313,77311,77331,77326,77318,77338,77336,77304,77314,77325,77328],"class_list":["post-26137","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-mathematical-algorithms","category-python","tag-colt-python","tag-excel-auto-number","tag-excel-python","tag-excel-python-tutorial","tag-excel-sheet-column-number","tag-excel-sheet-column-number-leetcode","tag-excel-worksheet-columns","tag-how-to-import-data-in-python","tag-how-to-parse-excel-file-in-python","tag-how-to-read-data-from-excel-file-in-python","tag-how-to-read-excel-file-in-python","tag-open-excel-in-python","tag-openpyxl-python","tag-openpyxl-tutorial","tag-pandas-create-dataframe","tag-pandas-excel","tag-pandas-excel-tutorial","tag-pandas-excelwriter","tag-pandas-table","tag-pandas-xlsx","tag-python-cell","tag-python-convert-excel-to-csv","tag-python-csv-file","tag-python-csv-writer","tag-python-excel","tag-python-excel-api","tag-python-excel-example","tag-python-excel-library","tag-python-excel-module","tag-python-excel-tutorial","tag-python-for-excel","tag-python-import-xls","tag-python-library-for-excel","tag-python-openpyxl","tag-python-openpyxl-tutorial","tag-python-pandas","tag-python-parse-text-file-to-csv","tag-python-read-excel","tag-python-to-excel","tag-python-workbook","tag-python-xlrd-examples","tag-python-xlrd-read-column","tag-python-xlrd-tutorial","tag-python-xls-parser","tag-python-xlsx","tag-read-excel","tag-read-file-in-python","tag-workbook-python","tag-xlrd","tag-xlrd-python-tutorial"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26137"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26137\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}