{"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=\u201dbanner\u201d]\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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20find%20Excel%20column%20name%20from%20a%20%0A%23%20given%20column%20number%0A%20%0AMAX%20%3D%2050%0A%20%0A%23%20Function%20to%20print%20Excel%20column%20name%20for%20a%20given%20column%20number%0Adef%20printString(n)%3A%0A%20%0A%20%20%20%20%23%20To%20store%20result%20(Excel%20column%20name)%0A%20%20%20%20string%20%3D%20%5B%22%5C0%22%5D*MAX%0A%20%0A%20%20%20%20%23%20To%20store%20current%20index%20in%20str%20which%20is%20result%0A%20%20%20%20i%20%3D%200%0A%20%0A%20%20%20%20while%20n%20%3E%200%3A%0A%20%20%20%20%20%20%20%20%23%20Find%20remainder%0A%20%20%20%20%20%20%20%20rem%20%3D%20n%2526%0A%20%0A%20%20%20%20%20%20%20%20%23%20if%20remainder%20is%200%2C%20then%20a%20\u2019Z\u2019%20must%20be%20there%20in%20output%0A%20%20%20%20%20%20%20%20if%20rem%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%5Bi%5D%20%3D%20\u2019Z\u2019%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20(n%2F26)-1%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20string%5Bi%5D%20%3D%20chr((rem-1)%20%2B%20ord(\u2018A\u2019))%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20n%2F26%0A%20%20%20%20string%5Bi%5D%20%3D%20\u2019%5C0\u2019%0A%20%0A%20%20%20%20%23%20Reverse%20the%20string%20and%20print%20result%0A%20%20%20%20string%20%3D%20string%5B%3A%3A-1%5D%0A%20%20%20%20print%20%22%22.join(string)%0A%20%0A%23%20Driver%20program%20to%20test%20the%20above%20Function%0AprintString(26)%0AprintString(51)%0AprintString(52)%0AprintString(80)%0AprintString(676)%0AprintString(702)%0AprintString(705)\u201d message=\u201dPython Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}