Convert multiple CSV files in a similar directory to XLSX files

I am converting multiple files CSV

under a similar directory to files XLSX

.

CSV

has a separator as a tab.

I ran the program and was able to generate files XLSX

. However, the files are XLSX

not tab-separated.

Please review my code and tell me what is wrong. On line 10, I specified my delimiter as a tab, but the resulting file is XLSX

not delimited.

import os
import glob
import csv
import openpyxl 

for csvfile in glob.glob(os.path.join(r'(my directory)', '*.csv')):
    wb = openpyxl.Workbook()
    ws = wb.active
    with open(csvfile, 'r') as f:
        reader = csv.reader(f, delimiter='\t')
        for r, row in enumerate(reader, start=1):
            for c, val in enumerate(row, start=1):
                ws.cell(row=r, column=c).value = val
    wb.save(csvfile + '.xlsx')

      

+3


source to share





All Articles