Skip to content

Added script for Excel to CSV Conversion #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Excel to CSV/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Excel to CSV Converter

## Description
This Python script converts Excel files (.xlsx, .xls) to CSV format. It can be used for batch processing of files and handles multiple Excel sheets.

## Prerequisites
- Python 3.x
- Libraries:
- pandas
- openpyxl (for .xlsx files)
40 changes: 40 additions & 0 deletions Excel to CSV/excel_to_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pandas as pd
import sys
import os

#excel_file: Path to the Excel file
#output_csv: Path to save the output CSV file
#sheet_name: Name of the sheet to convert. If None, the first sheet is used

def convert_excel_to_csv(excel_file, output_csv, sheet_name=None):


try:
#Checks if the file exists
if not os.path.exists(excel_file):
print(f"Error: The file '{excel_file}' does not exist.")
return

#Reads the Excel file
if sheet_name:
df = pd.read_excel(excel_file, sheet_name=sheet_name)
else:
df = pd.read_excel(excel_file)

# Converts the Excel file to CSV foormaat
df.to_csv(output_csv, index=False)
print(f"Successfully converted '{excel_file}' to '{output_csv}'.")

except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python excel_to_csv.py <input_excel_file> <output_csv_file> [sheet_name]")
else:
input_excel_file = sys.argv[1]
output_csv_file = sys.argv[2]
sheet_name = sys.argv[3] if len(sys.argv) > 3 else None
convert_excel_to_csv(input_excel_file, output_csv_file, sheet_name)