CSV Report Generator in Python.
About the project:
This CSV Report Generator project demonstrates how to create structured reports using Python and export them into a CSV (Comma-Separated Values) file. CSV files are widely used for storing and sharing tabular data across systems.
In this project, you will learn how to generate a report programmatically and save it in a format that can be opened using Excel, Google Sheets, or any spreadsheet application.
This will be a self-contained script that creates and writes data to a CSV file.
It is a useful tool for exporting data in a structured, spreadsheet-friendly format.
This program will allow you to define a list of data, and then it will write that data to a file named report.csv.
What You Will Learn from This Project
- How CSV files work and where they are used
- How to write structured data using Python’s
csvmodule - How to handle file paths and errors safely
- How to export reports for spreadsheet applications
- How to design reusable reporting scripts
How to use this program:
To use this CSV Report Generator, you simply need to save the code and run it. The csv library is a built-in module in Python, so you don't need to install anything.
Save the code: Save the code as a Python file (e.g., report_generator.py).
Run the script: Open your terminal or command prompt, navigate to the directory where you saved the file, and run python report_generator.py.
The program will create a file named report.csv in the same directory.
You can open this file with any spreadsheet program (like Microsoft Excel, Google Sheets, or LibreOffice Calc) to view the data.
You can easily modify the data list in the script to generate reports with different information.
Project Level: Beginner to Intermediate
Concepts Covered: CSV files, file handling, lists, exception handling, data export
How the CSV Report Generator Works
The script defines a function that creates a CSV file and writes multiple rows of data at once. Each row represents a record in the report.
- The
csvmodule handles CSV formatting automatically - A list of lists is used to represent rows and columns
- The file is created in write mode, replacing any existing file
- Error handling ensures the program fails gracefully if writing fails
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# csv_report_generator.py
import csv
import os
import sys
def generate_report(filename="report.csv"):
"""
Generates a CSV report file with some sample data.
Args:
filename (str): The name of the CSV file to create.
"""
# Sample data to be written to the CSV file
# Each inner list represents a row in the CSV
data = [
['Employee ID', 'Name', 'Department', 'Salary'],
['101', 'Alice', 'Engineering', '120000'],
['102', 'Bob', 'Marketing', '95000'],
['103', 'Charlie', 'Sales', '110000'],
['104', 'Diana', 'Engineering', '135000'],
['105', 'Eve', 'Finance', '100000'],
]
try:
# Open the file in write mode ('w')
with open(filename, 'w', newline='') as csvfile:
# Create a CSV writer object
writer = csv.writer(csvfile)
# Write all the rows at once
writer.writerows(data)
print(f"Successfully generated report: '{filename}'")
print(f"The file is located at: {os.path.abspath(filename)}")
except IOError as e:
print(f"Error writing to file '{filename}': {e}")
sys.exit(1)
def main():
"""
Main function to run the CSV Report Generator.
"""
print("--- Python CSV Report Generator ---")
generate_report()
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
Sample Output
After running the script, a file named report.csv will be created.
When opened in a spreadsheet application, it will look like this:
Employee ID | Name | Department | Salary ---------------------------------------------- 101 | Alice | Engineering | 120000 102 | Bob | Marketing | 95000 103 | Charlie | Sales | 110000 104 | Diana | Engineering | 135000 105 | Eve | Finance | 100000
Real-World Uses of CSV Report Generation
- Exporting employee or payroll reports
- Generating sales or finance summaries
- Sharing data between applications
- Creating backups of structured data
- Preparing datasets for data analysis
How You Can Extend This Project
- Accept user input instead of hardcoded data
- Read data from a database or API
- Add timestamps to the report filename
- Generate multiple CSV files dynamically
- Convert CSV reports into Excel or PDF formats
Conclusion
This CSV Report Generator project demonstrates how Python can be used to export structured data into a widely supported file format. It is a practical skill used in automation, reporting, and data analysis.
By understanding CSV generation, you gain the ability to share data across platforms and integrate Python scripts with real-world tools.
Related Python projects you may find useful:
← Back to Projects
