diff --git a/csv_to_excel/README.md b/csv_to_excel/README.md new file mode 100644 index 000000000..79e27c6cd --- /dev/null +++ b/csv_to_excel/README.md @@ -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/` diff --git a/csv_to_excel/main.py b/csv_to_excel/main.py new file mode 100644 index 000000000..698030121 --- /dev/null +++ b/csv_to_excel/main.py @@ -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) diff --git a/csv_to_excel/requirements.txt b/csv_to_excel/requirements.txt new file mode 100644 index 000000000..18e450228 --- /dev/null +++ b/csv_to_excel/requirements.txt @@ -0,0 +1,2 @@ +pandas==1.3.3 +openpyxl==3.0.9