import pandas as pd from jinja2 import Template # Read the CSV file df = pd.read_csv('sample_data.csv') # Calculate column totals (excluding the first column which contains row labels) totals = df.iloc[:, 1:].sum() # Create HTML template html_template = """ CSV Data Report

CSV Data Report

Raw Data

{{ raw_data | safe }}

Column Totals

{% for column in totals.index %} {% endfor %} {% for value in totals.values %} {% endfor %}
{{ column }}
{{ "{:,.2f}".format(value) }}
""" # Convert the raw data to HTML raw_data_html = df.to_html(index=False, classes='table') # Create the template and render it template = Template(html_template) html_output = template.render(raw_data=raw_data_html, totals=totals) # Save the HTML output with open('report.html', 'w') as f: f.write(html_output) print("Report has been generated as 'report.html'")