{"id":1538,"date":"2018-11-05T10:26:27","date_gmt":"2018-11-05T09:26:27","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1538"},"modified":"2018-11-05T10:26:27","modified_gmt":"2018-11-05T09:26:27","slug":"python-and-excel-part-1","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-and-excel-part-1\/","title":{"rendered":"Python and Excel (part 1)"},"content":{"rendered":"<p>This is the first one of a new serie of articles about the interaction between Python and Excel.<\/p>\n<h1>Learn to use Excel with Python<\/h1>\n<p>It&#8217;s a good thing to use Python to accelerate our work with Excel. Python is very easy to learn, it has the most beautiful syntax in the world of scripting \/programming languages and so, let&#8217;s learn how to use it to manipulate Excel data.<\/p>\n<h2>Installing XslxWriter<\/h2>\n<p>First of all let&#8217;s install the module <a href=\"https:\/\/xlsxwriter.readthedocs.io\/getting_started.html\">XlsxWriter<\/a> that enable us to interact with Escel through Python, right?<\/p>\n<pre class=\"lang:default decode:true \">pip install XlsxWriter<\/pre>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"410\" src=\"https:\/\/www.youtube.com\/embed\/UYP2E5ERyZQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h2>Create a new Excel file with Python<\/h2>\n<p>After you installed\u00a0 the XlsxWriter module, you are ready to create your Excel files with the data you need.<\/p>\n<p>You will follow this process:<\/p>\n<ul>\n<li>import the module<\/li>\n<li>create a Workbook<\/li>\n<li>create a worksheet<\/li>\n<li>add a value to a cell<\/li>\n<li>close the Workbook<\/li>\n<\/ul>\n<p>You can read the code here and in the video below.<\/p>\n<h2>The code to create an Excel file and add a value<\/h2>\n<pre class=\"lang:default decode:true \">import xlsxwriter\r\nimport os\r\n\r\nwb = xlsxwriter.Workbook(\"Excel1.xlsx\")\r\nws = wb.add_worksheet()\r\nws.write(\"A1\", 150)\r\nwb.close()\r\n\r\nos.system(\"Excel1.xlsx\")<\/pre>\n<h2>The video tutorial to use Excel<\/h2>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"410\" src=\"https:\/\/www.youtube.com\/embed\/iqxOROoj_tQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<script>\r\n\tvar title = \"Excel and Python\"\r\nvar links = [\r\n[\"python-and-excel-part-1\/\",\"Installing XlsxWriter and creating an Excel file\"]\r\n\t\t];\r\n<\/script>\r\n<script>\r\nvar next2 = 0;\r\n\thtml = \"<b style='color:coral;font-size:2em'>Other posts about \" + title + \"<\/b>\";\r\nfor (address of links) \r\n{\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='https:\/\/pythonprogramming.altervista.org\/\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (\"https:\/\/pythonprogramming.altervista.org\/\"+ address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nlet next = 0;\r\nlet addressStart = \"<a href='https:\/\/pythonprogramming.altervista.org\/\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\r\n\n","protected":false},"excerpt":{"rendered":"This is the first one of a new serie of articles about the interaction between Python and Excel. Learn to use Excel with \n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-and-excel-part-1\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":1541,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=1538"}],"version-history":[{"count":3,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1538\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1538\/revisions\/1542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/1541"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}