Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions csv_to_excel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Script to convert CSV -> excel

Script expect the user to pass path to CSV Or path to directory containing multiple CSV
and saves the excel file at same dir as provided path

## install requirements/dependencies:

python -r requirements.txt

## how to use:

call the program using:
`python main.py {filename or directory}`

file example: `python main.py test.csv`
dir example: `python main.py dirContainingCSVs/`
35 changes: 35 additions & 0 deletions csv_to_excel/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pandas as pd
import sys
import os

args = sys.argv[1:] # get command line *args
assert args, "No file/dir was provided" # raise error is no arg is passed

isdir = os.path.isdir(args[-1])

# If input is CSV file
if not isdir:
assert args[-1].endswith(".csv"), "Input file is not CSV; Provide CSV file"
data = pd.read_csv(args[-1]) # load CSV data as Pd object
data.to_excel(args[-1][:-4] + ".xlsx", index=None, header=True)
del data

# If input is Directory containing multiple csv
else:
try:
# remove "/" from end of directory is passed
if args[-1][-1] == "/":
args[-1] = args[-1][:-1]
# create a dir to save the excel datasheet
os.mkdir(args[-1] + "_excel")
except Exception:
pass
for filename in os.listdir(args[-1]):
if filename.endswith(".csv"):
data = pd.read_csv(filename)
data.to_excel(
args[-1] + "_excel/" + filename[:-4] + ".xlsx"
) # write as excel
del data # delete the pandas DataFrame object

exit(0)
2 changes: 2 additions & 0 deletions csv_to_excel/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pandas==1.3.3
openpyxl==3.0.9