🧩 Python Automation Recipes – 📊 CSV to Excel Report Generator
Posted on: January 12, 2026
Description:
📌 Introduction
CSV files are great for raw data, but not ideal for sharing with non-technical users.
Most real workflows need clean, formatted Excel reports — with proper sheets, headers, and basic styling.
This automation recipe shows how to convert CSV files into Excel reports automatically, making them easier to read, share, and archive.
This pattern is widely used in data pipelines, reporting jobs, and internal tooling.
🔎 Explanation
- The script reads a CSV file using pandas.
- It writes the data into an Excel (.xlsx) file using ExcelWriter.
- Basic formatting is applied:
- bold headers
- auto-adjusted column widths
- The output is a ready-to-share Excel report, not just a raw export.
You can extend this later with:
- multiple sheets
- charts
- filters
- summary rows
✅ Key Takeaways
- 📊 Convert raw CSV data into clean Excel reports.
- 🧾 Ideal for analytics, exports, and business sharing.
- ⚙️ Easy to extend with formatting or charts.
Code Snippet:
import pandas as pd
from pathlib import Path
# --- Step 1: Configure paths ---
# Input CSV file
CSV_FILE = Path("data.csv") # Set this to your CSV File
# Output Excel file
EXCEL_FILE = Path("report.xlsx") # Set this to your Excel File
# --- Step 2: Load CSV data ---
if not CSV_FILE.exists():
raise FileNotFoundError(f"CSV file not found: {CSV_FILE}")
df = pd.read_csv(CSV_FILE)
# --- Step 3: Write to Excel with formatting ---
with pd.ExcelWriter(EXCEL_FILE, engine="xlsxwriter") as writer:
df.to_excel(writer, index=False, sheet_name="Report")
workbook = writer.book
worksheet = writer.sheets["Report"]
# Format header row
header_format = workbook.add_format({
"bold": True,
"border": 1
})
for col_num, col_name in enumerate(df.columns):
worksheet.write(0, col_num, col_name, header_format)
# Auto-adjust column width
max_len = max(
df[col_name].astype(str).map(len).max(),
len(col_name)
)
worksheet.set_column(col_num, col_num, max_len + 2)
print(f"✅ Excel report generated successfully: {EXCEL_FILE}")
Link copied!
Comments
Add Your Comment
Comment Added!
No comments yet. Be the first to comment!