Skip to content

Instantly share code, notes, and snippets.

@songpon
Last active January 15, 2018 15:38
Show Gist options
  • Select an option

  • Save songpon/e9d2172c648ca2518c8cfaf7ea7a4c29 to your computer and use it in GitHub Desktop.

Select an option

Save songpon/e9d2172c648ca2518c8cfaf7ea7a4c29 to your computer and use it in GitHub Desktop.
convert csv to xlsx
#!/usr/bin/env python3
import sys
import csv
import xlsxwriter
"""
requirements:
pip install xlsxwriter
pip install progressbar2
usage:
./csv2xlsx.py file.csv
"""
from progressbar import Bar, ETA, Percentage, ProgressBar, SimpleProgress
# if we read f.csv we will write f.xlsx
wb = xlsxwriter.Workbook(sys.argv[1].replace(".csv",".xlsx"))
ws = wb.add_worksheet("WS1") # your worksheet title here
with open(sys.argv[1],'r') as csvfile:
table = [x for x in csv.reader(csvfile)]
i = 0
# write each row from the csv file as text into the excel file
# this may be adjusted to use 'excel types' explicitly (see xlsxwriter doc)
max_len= len(table)
widgets = ['CSV to Xlsx', Percentage(),' ', Bar(), ' ', ETA(), ' ' ,SimpleProgress()]
pbar = ProgressBar(widgets=widgets, maxval=max_len).start()
for row in table:
ws.write_row(i, 0, row)
i += 1
pbar.update(i)
pbar.finish()
wb.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment