Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active August 16, 2019 16:19
Show Gist options
  • Save Caffe1neAdd1ct/1bb2ad6d2a134e707dec37ffefad5e27 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/1bb2ad6d2a134e707dec37ffefad5e27 to your computer and use it in GitHub Desktop.
import os
import glob
import csv
import sys
from multiprocessing import Pool, freeze_support
from xlsxwriter.workbook import Workbook
def process_csv_to_xlsx(csvfile):
print('Processing: ' + csvfile)
workbook = Workbook(csvfile[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'rt', encoding='utf8') as f:
reader = csv.reader(f, delimiter='\t')
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
os.remove(csvfile)
if __name__ == '__main__':
if sys.platform.startswith('win'):
# Hack for multiprocessing.freeze_support() to work from a
# setuptools-generated entry point.
freeze_support()
print('Starting')
sourcePath = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'source')
print('Path to put CSV files: ' + sourcePath)
with Pool(processes=8) as pool:
if not os.path.exists(sourcePath):
os.makedirs(sourcePath)
ListOfFilenames = glob.glob(os.path.join(sourcePath, '*.csv'))
pool.map(process_csv_to_xlsx, ListOfFilenames)
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
base = 'Console'
executables = [
Executable('main.py', base=base, targetName = 'convert')
]
setup(name='CSV to XLSX',
version = '1.0',
description = '',
options = dict(build_exe = buildOptions),
executables = executables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment