{"id":5085,"date":"2020-02-18T18:49:18","date_gmt":"2020-02-18T17:49:18","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=5085"},"modified":"2020-02-18T18:49:18","modified_gmt":"2020-02-18T17:49:18","slug":"another-html-table-generator-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/another-html-table-generator-with-python\/","title":{"rendered":"Another html table generator with Python"},"content":{"rendered":"<p>I made some changes to my previous code to make html tables with python. Now you can easily edit the title and the header cells of the table<\/p>\n<p>Take a look at this code to make an html table:<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\nhtml = \"\"\"&lt;style&gt;\r\n@import \"https:\/\/fonts.googleapis.com\/css?family=Montserrat:300,400,700\"; .rwd-table {margin: 1em 0; min-width: 300px; } .rwd-table tr {border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; } .rwd-table th {display: none; } .rwd-table td {display: block; } .rwd-table td:first-child {padding-top: .5em; } .rwd-table td:last-child {padding-bottom: .5em; } .rwd-table td:before {content: attr(data-th) \": \"; font-weight: bold; width: 6.5em; display: inline-block; } @media (min-width: 480px) {.rwd-table td:before {display: none; } } .rwd-table th, .rwd-table td {text-align: left; } @media (min-width: 480px) {.rwd-table th, .rwd-table td {display: table-cell; padding: .25em .5em; } .rwd-table th:first-child, .rwd-table td:first-child {padding-left: 0; } .rwd-table th:last-child, .rwd-table td:last-child {padding-right: 0; } } h1 {font-weight: normal; letter-spacing: -1px; color: #34495E; } .rwd-table {background: #34495E; color: #fff; border-radius: .4em; overflow: hidden; } .rwd-table tr {border-color: #46637f; } .rwd-table th, .rwd-table td {margin: .5em 1em; } @media (min-width: 480px) {.rwd-table th, .rwd-table td {padding: 1em !important; } } .rwd-table th, .rwd-table td:before {color: #dd5; } &lt;\/style&gt;\r\n&lt;script&gt;\r\n  window.console = window.console || function(t) {};\r\n&lt;\/script&gt;\r\n&lt;script&gt;\r\n  if (document.location.search.match(\/type=embed\/gi)) {\r\n    window.parent.postMessage(\"resize\", \"*\");\r\n  }\r\n&lt;\/script&gt;\r\n\r\n\r\n&lt;h1&gt;Libro giornale&lt;\/h1&gt;\r\n&lt;table class=\"rwd-table\"&gt;\r\n&lt;tr&gt;\r\n\r\n  &lt;table class=\"rwd-table\"&gt;\r\n&lt;tr&gt;\r\n&lt;---intestazione---&gt;\r\n&lt;\/tr&gt;\r\n&lt;---conti---&gt;\r\n\r\n\r\n&lt;\/table&gt;\"\"\"\r\n\r\n# ================================== Python ===\r\n\r\n# changine the first line\r\n\r\ndef first_line(fl):\r\n    fl = fl.split(\",\")\r\n    intestazione1 = \"&lt;---intestazione---&gt;\"\r\n    intestazione2 = \"&lt;th&gt;{}&lt;\/th&gt;&lt;th&gt;{}&lt;\/th&gt;&lt;th&gt;{}&lt;\/th&gt;&lt;th&gt;{}&lt;\/th&gt;\"\r\n    intestazione2 = intestazione2.format(*fl)\r\n    return intestazione1, intestazione2\r\n\r\n\r\n#              --- data of the table ---\r\ntitle = \"Piano degli investimenti\"\r\nfl = \"Bene, Costo storico, anni, ammortamento\"\r\nreplace = \"\"\"\r\nSoftware, 10.000, 5, 2.000,\r\narredamenti, 50.000, 16, 3.125,\r\nattrezzature, 3000, 8, 3.750,\r\nimpianti, 25.000, 10, 2.500,\r\n\"\"\"\r\n\r\nhtml = html.replace(\"Libro giornale\", title)\r\nintestazione1, intestazione2 = first_line(fl)\r\nhtml = html.replace(intestazione1, intestazione2)\r\n\r\nreplace = replace.split(\",\")\r\nlines = len(replace) \/\/ 4\r\n\r\nconto = \"\"\"\r\n&lt;tr&gt;\r\n&lt;td data-th=\"Movie Title\"&gt;{}&lt;\/td&gt;\r\n&lt;td data-th=\"Genre\"&gt;{}&lt;\/td&gt;\r\n&lt;td data-th=\"Year\"&gt;{}&lt;\/td&gt;\r\n&lt;td data-th=\"Gross\"&gt;{}&lt;\/td&gt;\r\n&lt;\/tr&gt;\"\"\"*lines\r\n\r\n\r\nprint(replace)\r\nconto = conto.format(*replace)\r\nhtml = html.replace(\"&lt;---conti---&gt;\", conto)\r\n\r\nwith open(myfile := \"table.html\", \"w\") as file:\r\n    file.write(html)\r\n\r\nos.startfile(myfile)<\/pre>\n<p>This is the output<\/p>\n\r\n<style>\r\n@import \"https:\/\/fonts.googleapis.com\/css?family=Montserrat:300,400,700\"; .rwd-table {margin: 1em 0; min-width: 300px; } .rwd-table tr {border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; } .rwd-table th {display: none; } .rwd-table td {display: block; } .rwd-table td:first-child {padding-top: .5em; } .rwd-table td:last-child {padding-bottom: .5em; } .rwd-table td:before {content: attr(data-th) \": \"; font-weight: bold; width: 6.5em; display: inline-block; } @media (min-width: 480px) {.rwd-table td:before {display: none; } } .rwd-table th, .rwd-table td {text-align: left; } @media (min-width: 480px) {.rwd-table th, .rwd-table td {display: table-cell; padding: .25em .5em; } .rwd-table th:first-child, .rwd-table td:first-child {padding-left: 0; } .rwd-table th:last-child, .rwd-table td:last-child {padding-right: 0; } } h1 {font-weight: normal; letter-spacing: -1px; color: #34495E; } .rwd-table {background: #34495E; color: #fff; border-radius: .4em; overflow: hidden; } .rwd-table tr {border-color: #46637f; } .rwd-table th, .rwd-table td {margin: .5em 1em; } @media (min-width: 480px) {.rwd-table th, .rwd-table td {padding: 1em !important; } } .rwd-table th, .rwd-table td:before {color: #dd5; } <\/style>\r\n<script>\r\n  window.console = window.console || function(t) {};\r\n<\/script>\r\n<script>\r\n  if (document.location.search.match(\/type=embed\/gi)) {\r\n    window.parent.postMessage(\"resize\", \"*\");\r\n  }\r\n<\/script>\r\n\r\n\r\n<h1>Piano degli investimenti<\/h1>\r\n<table class=\"rwd-table\">\r\n<tr>\r\n\r\n  <table class=\"rwd-table\">\r\n<tr>\r\n<th>Bene<\/th><th> Costo storico<\/th><th> anni<\/th><th> ammortamento<\/th>\r\n<\/tr>\r\n\r\n<tr>\r\n<td data-th=\"Movie Title\">\r\nSoftware<\/td>\r\n<td data-th=\"Genre\"> 10.000<\/td>\r\n<td data-th=\"Year\"> 5<\/td>\r\n<td data-th=\"Gross\"> 2.000<\/td>\r\n<\/tr>\r\n<tr>\r\n<td data-th=\"Movie Title\">\r\narredamenti<\/td>\r\n<td data-th=\"Genre\"> 50.000<\/td>\r\n<td data-th=\"Year\"> 16<\/td>\r\n<td data-th=\"Gross\"> 3.125<\/td>\r\n<\/tr>\r\n<tr>\r\n<td data-th=\"Movie Title\">\r\nattrezzature<\/td>\r\n<td data-th=\"Genre\"> 3000<\/td>\r\n<td data-th=\"Year\"> 8<\/td>\r\n<td data-th=\"Gross\"> 3.750<\/td>\r\n<\/tr>\r\n<tr>\r\n<td data-th=\"Movie Title\">\r\nimpianti<\/td>\r\n<td data-th=\"Genre\"> 25.000<\/td>\r\n<td data-th=\"Year\"> 10<\/td>\r\n<td data-th=\"Gross\"> 2.500<\/td>\r\n<\/tr>\r\n\r\n\r\n<\/table>\n","protected":false},"excerpt":{"rendered":"How to make an html table throug a python script\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/another-html-table-generator-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[321,4,320],"class_list":["post-5085","post","type-post","status-publish","format-standard","hentry","category-examples","tag-html","tag-python","tag-table"],"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\/5085","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=5085"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/5085\/revisions"}],"predecessor-version":[{"id":5090,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/5085\/revisions\/5090"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=5085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=5085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=5085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}