Another html table generator with Python

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

Take a look at this code to make an html table:

import os

html = """<style>
@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>
<script>
  window.console = window.console || function(t) {};
</script>
<script>
  if (document.location.search.match(/type=embed/gi)) {
    window.parent.postMessage("resize", "*");
  }
</script>


<h1>Libro giornale</h1>
<table class="rwd-table">
<tr>

  <table class="rwd-table">
<tr>
<---intestazione--->
</tr>
<---conti--->


</table>"""

# ================================== Python ===

# changine the first line

def first_line(fl):
    fl = fl.split(",")
    intestazione1 = "<---intestazione--->"
    intestazione2 = "<th>{}</th><th>{}</th><th>{}</th><th>{}</th>"
    intestazione2 = intestazione2.format(*fl)
    return intestazione1, intestazione2


#              --- data of the table ---
title = "Piano degli investimenti"
fl = "Bene, Costo storico, anni, ammortamento"
replace = """
Software, 10.000, 5, 2.000,
arredamenti, 50.000, 16, 3.125,
attrezzature, 3000, 8, 3.750,
impianti, 25.000, 10, 2.500,
"""

html = html.replace("Libro giornale", title)
intestazione1, intestazione2 = first_line(fl)
html = html.replace(intestazione1, intestazione2)

replace = replace.split(",")
lines = len(replace) // 4

conto = """
<tr>
<td data-th="Movie Title">{}</td>
<td data-th="Genre">{}</td>
<td data-th="Year">{}</td>
<td data-th="Gross">{}</td>
</tr>"""*lines


print(replace)
conto = conto.format(*replace)
html = html.replace("<---conti--->", conto)

with open(myfile := "table.html", "w") as file:
    file.write(html)

os.startfile(myfile)

This is the output

Piano degli investimenti

Bene Costo storico anni ammortamento
Software 10.000 5 2.000
arredamenti 50.000 16 3.125
attrezzature 3000 8 3.750
impianti 25.000 10 2.500

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.