DEV Community

IntelliTools
IntelliTools

Posted on

Generate HTML Reports with Python in 10 Lines of Code

I've spent way too much time manually copying data from spreadsheets into HTML tables. It's tedious, error-prone, and just plain annoying. That's why I built the HTML Report Generator — a simple Python script that turns CSV or Excel files into clean, styled HTML reports in seconds.

This tool is perfect for developers, analysts, and anyone who needs to generate reports for clients or internal use. You can run it from the command line, schedule it with cron or Task Scheduler, and even integrate it into your CI/CD pipeline.

Let's walk through a simple example. Suppose you have a CSV file named sales_data.csv with columns like Date, Product, and Amount. Here's how you can generate an HTML report from it:

import csv
from html_report import generate_report

generate_report(
    input_file='sales_data.csv',
    output_file='sales_report.html',
    title='Sales Report',
    table_classes='table table-striped'
)
Enter fullscreen mode Exit fullscreen mode

This script reads the CSV file, creates a styled HTML table, and saves it as sales_report.html. The html_report module handles all the HTML generation, so you don't have to write any HTML or CSS.

Under the hood, the script uses Python's built-in csv module to parse the input file and the html_report module to generate the HTML. The html_report module is a small library I wrote that wraps up all the HTML generation logic, making it easy to use for anyone.

Here's a quick look at how the HTML output looks:

<!DOCTYPE html>
<html>
<head>
    <title>Sales Report</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Sales Report</h1>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Date</th>
                <th>Product</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>2023-04-01</td>
                <td>Widget A</td>
                <td>$100</td>
            </tr>
            <!-- More rows -->
        </tbody>
    </table>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The generated HTML includes basic styling for readability, and you can easily customize the CSS by adding a styles.css file in the same directory.

This script is incredibly useful for anyone who needs to generate reports regularly. It's a great example of how a small automation tool can save hours of manual work. You can use it for anything from tracking project progress to summarizing user activity.

If you're looking for a quick way to generate HTML reports from your data, this script is a solid starting point. You can grab the full script here: https://intellitools.gumroad.com/l/html-report-generator

What's the biggest time-saver you've found in your automation workflow? I'd love to hear your thoughts!

Top comments (0)