The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Convert CSV to styled HTML reports

To convert a CSV file to a styled HTML report, you can follow this process using Python and Pandas with basic HTML and CSS for styling. Below is a script that takes a CSV file and generates an HTML file with a clean, styled table layout.


Python Script to Convert CSV to Styled HTML

python
import pandas as pd # Load CSV file csv_file = "data.csv" # Replace with your CSV file path df = pd.read_csv(csv_file) # Define custom HTML style style = """ <style> body { font-family: Arial, sans-serif; padding: 20px; } h2 { text-align: center; color: #333; } table { width: 100%; border-collapse: collapse; margin-top: 20px; font-size: 14px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #4CAF50; color: white; } tr:nth-child(even) { background-color: #f9f9f9; } tr:hover { background-color: #f1f1f1; } </style> """ # Convert DataFrame to HTML with style html_content = f""" <html> <head>{style}</head> <body> <h2>CSV Report</h2> {df.to_html(index=False, escape=False, classes='table')} </body> </html> """ # Save HTML file output_file = "report.html" with open(output_file, "w", encoding="utf-8") as file: file.write(html_content) print(f"HTML report generated: {output_file}")

Notes:

  • Replace "data.csv" with the path to your actual CSV file.

  • You can modify the CSS in the style variable to match your branding or design preferences.

  • The table includes alternating row colors and hover effects for better readability.

Let me know if you’d like to embed images, charts, or conditional formatting in the report as well.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About