File size: 1,739 Bytes
			
			| e59dc66 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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 = """
<!DOCTYPE html>
<html>
<head>
    <title>CSV Data Report</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: right;
        }
        th {
            background-color: #f2f2f2;
        }
        .total-row {
            font-weight: bold;
            background-color: #e6e6e6;
        }
        h1 {
            color: #333;
        }
    </style>
</head>
<body>
    <h1>CSV Data Report</h1>
    
    <h2>Raw Data</h2>
    {{ raw_data | safe }}
    
    <h2>Column Totals</h2>
    <table>
        <tr>
            {% for column in totals.index %}
            <th>{{ column }}</th>
            {% endfor %}
        </tr>
        <tr class="total-row">
            {% for value in totals.values %}
            <td>{{ "{:,.2f}".format(value) }}</td>
            {% endfor %}
        </tr>
    </table>
</body>
</html>
"""
# 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'")  |