{"id":1524,"date":"2023-02-08T20:00:00","date_gmt":"2023-02-08T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1524"},"modified":"2023-01-09T11:09:13","modified_gmt":"2023-01-09T02:09:13","slug":"list-excel","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/list-excel\/","title":{"rendered":"[Python] How to Write List to Excel [openpyxl, pandas]"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to write list to Excel<\/span>.<\/p>\n\n\n\n<p>Use &#8220;openpyxl&#8221;, a library for Excel manipulation in Python.<\/p>\n\n\n\n<p>As a supplement, I also show how to write a list to Excel using &#8220;pandas&#8221;.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>How to use &#8220;openpyxl&#8221;<\/li><li>How to write list to Excel using &#8220;openpyxl&#8221;<\/li><li>How to write list to Excel using &#8220;pandas&#8221;<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to use &#8220;openpyxl&#8221;<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">&#8220;openpyxl&#8221; is the most common library for Excel manipulation in Python<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the beginning, I show basic usage of &#8220;openpyxl&#8221;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Installing &#8220;openpyxl&#8221; in Python<\/h3>\n\n\n\n<p>&#8220;openpyxl&#8221; is external library.<\/p>\n\n\n\n<p>External libraries in Python can also be downloaded and installed using pip which is a package manager.<\/p>\n\n\n\n<p>To  install &#8220;openpyxl&#8221;, you run the following in your terminal or command line.<\/p>\n\n\n\n<p class=\"is-style-default has-st-border has-st-solid-border has-st-thick-border-width\">&gt; pip install openpyxl<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Basic usage of &#8220;openpyxl&#8221;<\/h3>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">basic usage of &#8220;openpyxl&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code to <span class=\"st-mymarker-s\">create a new Excel Workbook<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import openpyxl\n\npath_excel = r'C:\\***** any path *****\\test.xlsx'\n\nwb = openpyxl.Workbook()\nsheet = wb.worksheets&#091;0]\n\nsheet.cell(2, 1, 'Apple')\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>> path_excel = r&#8217;C:***** any path *****\\test.xlsx&#8217;<\/p>\n\n\n\n<p>Specify the path of the new Excel file.<\/p>\n\n\n\n<p>By prefixing a string literal with &#8220;r&#8221;, you can create a raw string in Python.<\/p>\n\n\n\n<p>&#8220;Raw string&#8221; treats the backslash character &#8220;\\n&#8221; as a literal character, not newline.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; wb = openpyxl.Workbook()<\/p>\n\n\n\n<p>A new Excel file is created.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; sheet = wb.worksheets[0]<\/p>\n\n\n\n<p>The 0th Excel sheet is specified.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>> sheet.cell(2, 1, &#8216;Apple&#8217;)<\/p>\n\n\n\n<p>The string &#8220;Apple&#8221; is written to the second row and first column of the sheet specified earlier.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; wb.save(path_excel)<\/p>\n\n\n\n<p>The Excel file is saved.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; wb.close()<\/p>\n\n\n\n<p>The Excel file is closed.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Write list into existing Excel file<\/h3>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code to write list into existing Excel file.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import openpyxl\n\npath_excel = r'C:\\***** any path *****\\test.xlsx'\n\nwb = openpyxl.load_workbook(path_excel)\nsheet = wb.worksheets&#091;0]\n\nsheet.cell(3, 1, 'Banana')\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; wb = openpyxl.load_workbook(path_excel)<\/p>\n\n\n\n<p>Existing Excel file is opened.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">How to write list to Excel using &#8220;openpyxl&#8221;<\/h2>\n\n\n\n<p>So, to get to the point.<\/p>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to write list to Excel using &#8220;openpyxl&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I show the following two ways to write list to Excel.<\/p>\n\n\n\n<ul><li>Using sheet.append( )<\/li><li>Using user-defined function<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using sheet.append( )<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">Using &#8220;sheet.append( )&#8221;, the list is written to Excel line by line<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import openpyxl\n\nlist_2D = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\npath_excel = r'C:\\***** any path *****\\test.xlsx'\n\nwb = openpyxl.Workbook()\nsheet = wb.worksheets&#091;0]\n\nfor row in list_2D :\n    sheet.append(row)\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; for row in list_2D :<br>&gt;     sheet.append(row)<\/p>\n\n\n\n<p>Rows are taken one by one from list_2D and written to Excel.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using user-defined function<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">Using &#8220;user-defined function&#8221;, the list is written to Excel.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import openpyxl\n\ndef write_list_2d(sheet, l_2d, start_row, start_col):\n    for y, row_data in enumerate(l_2d):\n        for x, cell_data in enumerate(row_data):\n            sheet.cell(row=start_row + y, column=start_col + x, value=l_2d&#091;y]&#091;x])\n\nlist_2D = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\npath_excel = r'C:\\***** any path *****\\test.xlsx'\n\nwb = openpyxl.Workbook()\nsheet = wb.worksheets&#091;0]\n\nwrite_list_2d(sheet, list_2D, 2, 1)\n\nwb.save(path_excel)\nwb.close()<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Using &#8220;def statement&#8221;, a function is defined that takes the elements from list one by one and writes them to Excel.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; write_list_2d(sheet, list_2D, 2, 1)<\/p>\n\n\n\n<p>Arguments are passed to user-defined function.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I referred to <a href=\"https:\/\/note.nkmk.me\/python-openpyxl-usage\/\" target=\"_blank\" rel=\"noreferrer noopener\">this site<\/a>.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">How to write list to Excel using &#8220;pandas&#8221;<\/h2>\n\n\n\n<p>As a supplement, I show <span class=\"st-mymarker-s\">how to use pandas to write list to Excel<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import pandas as pd\n\nlist_2D = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\npath_excel = r'C:\\***** any path *****\\test.xlsx'\n\ndf = pd.DataFrame(list_2D)\n\ndf.to_excel(path_excel, sheet_name='test', index=False, header=False, startrow=1, startcol=0)<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; df = pd.DataFrame(list_2D)<\/p>\n\n\n\n<p>The list is converted to a DataFrame object.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; df.to_excel(path_excel, sheet_name=&#8217;test&#8217;, index=False, header=False, startrow=1, startcol=0)<\/p>\n\n\n\n<p>DataFrame object is output to Excel.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to write list to Excel. Use &#8220;openpyxl&#8221;, a library for Excel manip &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2907,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,5,11],"tags":[16,20,19],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1524"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1524"}],"version-history":[{"count":124,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1524\/revisions"}],"predecessor-version":[{"id":3195,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1524\/revisions\/3195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/2907"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}