DEV Community

Cover image for How to Convert Excel Spreadsheets to CSV with Python
Oddshop
Oddshop

Posted on • Originally published at oddshop.work

How to Convert Excel Spreadsheets to CSV with Python

Processing large Excel files manually wastes hours of developer time when simple automation could handle the work. A python spreadsheet converter can eliminate the tedious cycle of opening files, selecting ranges, and exporting subsets of data repeatedly.

The Manual Way (And Why It Breaks)

Opening Excel files to manually select columns and filter rows becomes unsustainable when dealing with regular data processing tasks. You start by launching Excel, waiting for it to load, then carefully selecting specific columns while ensuring you don't miss any required fields. The excel to csv python approach eliminates this bottleneck, but basic scripts often lack proper error handling and data type preservation. When working with multiple worksheets or complex filtering conditions, manual processing leads to human errors, inconsistent formatting, and wasted time that could be better spent on actual analysis work.

The Python Approach

Here's a minimal snippet for basic spreadsheet processing:

import pandas as pd
import sys

def quick_convert(input_file, output_file, sheet_name=0):
    # Read the Excel file
    df = pd.read_excel(input_file, sheet_name=sheet_name)

    # Basic column selection (first 3 columns as example)
    selected_cols = df.iloc[:, :3]

    # Simple row filtering (rows where first column > 50)
    filtered_df = selected_cols[selected_cols.iloc[:, 0] > 50]

    # Export to CSV
    filtered_df.to_csv(output_file, index=False)
    print(f"Converted {len(filtered_df)} rows to {output_file}")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python script.py input.xlsx output.csv")
        sys.exit(1)
    quick_convert(sys.argv[1], sys.argv[2])
Enter fullscreen mode Exit fullscreen mode

This basic script handles simple column selection by position and basic filtering operations. However, it lacks support for named column selection, complex filtering conditions, multiple worksheet handling, and proper error management that production workflows require.

What the Full Tool Handles

• Convert XLSX/XLS files to CSV format preserving data types
• Select specific columns by name or index position

• Filter rows using custom conditions and value matching
• Support for multiple worksheets within single Excel files
• Output to console or save filtered results to new CSV file

The complete python spreadsheet converter handles edge cases like merged cells, different data formats, and missing values that break simpler implementations during spreadsheet automation python workflows.

Running It

python excel_filter.py --input data.xlsx --columns A,C,E --filter "B>100" --output filtered.csv
Enter fullscreen mode Exit fullscreen mode

The command accepts input file path, comma-separated column references (by letter or name), filter expressions using standard operators, and output destination. Results include filtered data with preserved formatting and appropriate data types for downstream processing.

Get the Script

Skip the build process and get immediate access to the full solution.

Download Spreadsheet to CSV Filter Converter →

$29 one-time. No subscription. Works on Windows, Mac, and Linux.


Built by OddShop — Python automation tools for developers and businesses.

Top comments (0)