import pyexcel
import glob
import sys
import re
import os
# $ python script.py arg1 [arg2]
# # arg2 optional, arg1 not. arg1 a path to dir or *.csv or specific.csv
# arg2 can be -merged or specfic.xlsx
# if multiple infiles are seen, then if no arg2, each file will result in unique .xlsx
if ( os.path.isdir( sys.argv[1] ) ) :
infiles = glob.glob( sys.argv[1] + '/*.csv' )
elif ( re.search( '.csv' , sys.argv[1] ) ) :
infiles = glob.glob( sys.argv[1] )
else :
sys.exit()
if ( len( sys.argv) > 2 ) :
if ( re.search( '-merged' , sys.argv[2] ) ) :
pyexcel.merge_csv_to_a_book( infiles , 'merged.xlsx' )
elif( re.search( '.xlsx' , sys.argv[2] ) ) :
pyexcel.merge_csv_to_a_book( infiles , sys.argv[2] )
else :
for file in infiles :
pyexcel.merge_csv_to_a_book( [file] , file.replace( '.csv' , '.xlsx' ) )
No comments:
Post a Comment