How to Convert Excel to CSV Using Python

How to Convert Excel to CSV Using Python

Excel files (.xlsx or .xls) are widely used for data storage, but CSV (Comma-Separated Values) files are simpler and more compatible with various applications. Python makes it easy to convert Excel files to CSV format using the pandas library, which is the most popular and efficient tool for data manipulation.


Prerequisites

Before proceeding, ensure you have the following installed:

  • Python 3.x – The latest version is recommended.
  • pandas – A powerful data analysis library.
  • openpyxl or xlrd – Required for reading Excel files.

Install the necessary libraries using pip:

pip install pandas openpyxl

Step-by-Step Conversion Process

1. Import the Required Libraries

First, import the pandas library in your Python script:

import pandas as pd

2. Read the Excel File

Use pandas.read_excel() to load the Excel file into a DataFrame:

df = pd.read_excel('input_file.xlsx', engine='openpyxl')

Note: If you're using an older .xls file, replace engine='openpyxl' with engine='xlrd'.

3. Save the DataFrame as CSV

Convert the DataFrame to a CSV file using df.to_csv():

df.to_csv('output_file.csv', index=False)

Explanation:

  • index=False ensures that row numbers are not included in the CSV.
  • You can specify a different delimiter (e.g., sep=';') if needed.

Complete Example

Here’s a full script to convert an Excel file to CSV:

import pandas as pd

# Read Excel file
df = pd.read_excel('input_file.xlsx', engine='openpyxl')

# Save as CSV
df.to_csv('output_file.csv', index=False)

print("Excel file converted to CSV successfully!")

Handling Multiple Sheets

If your Excel file has multiple sheets, you can specify the sheet name or index:

# Read a specific sheet by name
df = pd.read_excel('input_file.xlsx', sheet_name='Sheet1', engine='openpyxl')

# Read a specific sheet by index (0-based)
df = pd.read_excel('input_file.xlsx', sheet_name=0, engine='openpyxl')

To convert all sheets into separate CSV files, use a loop:

excel_file = pd.ExcelFile('input_file.xlsx', engine='openpyxl')

for sheet_name in excel_file.sheet_names:
    df = pd.read_excel(excel_file, sheet_name=sheet_name)
    df.to_csv(f'{sheet_name}.csv', index=False)

Summary: Converting Excel to CSV in Python is simple with the pandas library. This method ensures data integrity and works seamlessly for single or multiple sheets. Ideal for automation, data analysis, and integration tasks.

Incoming search terms
- How to convert Excel to CSV using Python pandas
- Best way to export Excel to CSV in Python
- Python script to convert XLSX to CSV
- Convert multiple Excel sheets to CSV files
- Pandas Excel to CSV conversion guide
- How to save DataFrame as CSV in Python
- Automate Excel to CSV conversion with Python
- Read Excel file and export as CSV in Python
- Python code for converting XLS to CSV
- Handling Excel files with pandas in Python

No comments:

Post a Comment