{}const=>[]async()letfn</>var
PythonDevelopment

HTML reports from Python: simple and beautiful

Learn how to save data from scripts in HTML — for reports, charts, and automation.

К

Kodik

Author

3 min read

If your Python script processes data, logs errors, or counts metrics, a logical question arises: how convenient is it to save the result?
The ideal format is HTML. It is beautiful, flexible and opens in any browser. And it's easy to generate on the fly.

In this article, we will show you how to quickly generate HTML reports in Python. Without frameworks and unnecessary magic — only practical techniques 💡

🧰 When do you need an HTML report?

  • After processing CSV/Excel data

  • To display graphs and tables

  • When collecting metrics (for example, in cron tasks)

  • For error logs, processed nicely

  • For storing project reports

📊 Advantages of HTML:

  • Easy to visualize

  • Supports styles and graphics

  • Opens on any device

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

🛠 Ways to generate HTML

Method

Suitable for

🔹 Manual line assembly

Simple cases

🔹 Template engines (Jinja2)

Flexible templates

🔹 Pandas .to_html()

Tables

🔹 Libraries (WeasyPrint, yattag)

Advanced layout

🧪 Example 1: Simple HTML manually

html = """
<!DOCTYPE html>
<html>
<head><title>Report</title></head>
<Body>
 <h1> Sample report </ h1>
  <p>Total users: 25</p>
</body>
</html>
"""

with open("report.html", "w", encoding="utf-8") as f:
    f.write(html)

🧪 Example 2: Pandas .to_html() for tables

import pandas as pd

df = pd.DataFrame({
    "Name": ["Code", "Alice", "John"],
    "Points": [85, 90, 78]
})

html_table = df.to_html(index=False)

html = f"""
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    table {{ border-collapse: collapse; width: 50%; margin: 20px auto; }}
    th, td {{ border: 1px solid #ccc; padding: 8px; text-align: center; }}
    th {{ background-color: #f2f2f2; }}
  </style>
</head>
<body>
  <h2 style="text-align:center;"> Score table </h2>
  {html_table}
</body>
</html>
"""

with open("report.html", "w", encoding="utf-8") as f:
    f.write(html)

🧪 Example 3: Jinja2 — templates with data

from jinja2 import Environment, FileSystemLoader

data = {"users": [{"name": "Code", "score": 88}, {"name": "Lena", "score": 95}]}

env = Environment(loader=FileSystemLoader("templates"))
template = env.get_template("report.html")

html = template.render(users=data["users"])

with open("report.html", "w", encoding="utf-8") as f:
    f.write(html)

Template templates/report.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Отчёт</title>
</head>
<body>
  <h1>Баллы пользователей</h1>
  <ul>
    {% for user in users %}
      <li>{{ user.name }} — {{ user.score }}</li>
    {% endfor %}
  </ul>
</body>
</html>

📦 How to add schedules?

If you use matplotlib or plotly, you can save the schedule as an image and insert it into the report:

plt.savefig("chart.png")

html = """
<img src="chart.png" alt="График">
"""

💡 Where an HTML report comes in handy

  • 📊 Internal team reports

  • 🛠 DevOps monitoring (via cron)

  • 💼 Automatic reports to clients

  • 📁 Archiving script results

🤖 Can I have a PDF?

Yes! Use WeasyPrint or pdfkit:

pip install weasyprint
from weasyprint import HTML

HTML("report.html").write_pdf("report.pdf")

In the attachment Code you will find courses in Python, HTML and others.
Create templates, visualize data, and make everything beautiful — even if you're just starting out.

Join our Telegram communities 📬

💬 Write if you want to see an article about PDF generation, interactive reports, or automatic email newsletters with HTML reports!

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card