CSV Report Generator Project (code) in Python

← Back to Projects

CSV Report Generator in Python.

About the project: This is a CSV Report Generator in Python.

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.


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: Intermediate

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 steps

Step 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()