I am looking for a way to quickly convert a directory of .csv files into the .xls or .xlsx format (would be nice if I could do either/or).
Is there an easy way to do this or do I need to install a third party program?
37 Answers
Assuming you like and have Python (for some reason), you could use this script I whipped up:
import os
import glob
import csv
import xlwt # from
for csvfile in glob.glob(os.path.join('.', '*.csv')): wb = xlwt.Workbook() ws = wb.add_sheet('data') with open(csvfile, 'rb') as f: reader = csv.reader(f) for r, row in enumerate(reader): for c, val in enumerate(row): ws.write(r, c, val) wb.save(csvfile + '.xls')Ran in the directory with all the CSV files, it will convert them all and slap a ".xls" onto the end.
For Excel 2007+ (xlsx files) supporting up to about 1 Mrows:
import os
import glob
import csv
import openpyxl # from or PyPI (e.g. via pip)
for csvfile in glob.glob(os.path.join('.', '*.csv')): wb = openpyxl.Workbook() ws = wb.active with open(csvfile, 'rb') as f: reader = csv.reader(f) 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') 1 Here is a perl script that supposedly does it, but it does seem like a lot of work to do something that is already built into Excel.
0There is a lot left unsaid in your question.
Assuming that your CVS files are in a directory structure similar to
c:\randompath\CSV\ a.csv b.csv c.csv : : z.csvand you want to end up with
c:\randompath\XLS\ a.xls b.xls c.xls : : z.xlsI can think of three routes to go depending on the ratio of upfront work to clean-up work you are willing to do.
- No Precoding: Use Windows Explorer to navigate to the CSV files use whatever method works best to select the files to be converted (lasso, ctrl+a, ctrl+click, shift+click) then right click one of the selected files and click on Open. This will open all the files in Excel. Then for each file you can select "File" and "Save as" and finally choose the new file format you want it saved to.
- A Simple Batch File: That batch file could use wild cards and/or a for each loop structure to open each of the CSV files for you and then you could manually process them as before.
- Create a VBA program within an Excel Spreadsheet: VBA could automatically opens each CSV file and then saves it to an Excel format. You could even add a simple message box that offers a choice of either xls or xlsx as each file is opened.
Note that I haven't written any code to do these things (yet) I am just offering ideas for a starting point. Perhaps if you could give more details about what you want you could get more detailed explanations for how to handle approach from me or another forum member.
I sympathise with you! I had to do this same conversion. I was trying to manipulate data using openpyxl but for some reason it wasn't doing what I wanted with with csv files so had to convert to xlsx first.
I used Python and Pandas. I am using Python 3.
import pandas as pd
import os
for (dirname, dirs, files) in os.walk('.'): #source directory
for filename in files: if filename.endswith('.csv'): df = pd.read_csv(filename) df.to_excel(filename + '.xlsx') os.remove(filename) #deletes csv file 1 For Windows? CoolUtils "Total CSV Converter" command line version supports many output formats including JSON, Access, DBF, XML & SQL and is only $40. It can recurse sub-directories, delete original CSV files, combine all files to one document, and more.
CSVConverter.exe <source> <destination> <options>"Advanced CSV Converter" ($40-200) is a portable EXE that can do it quickly and without Excel having to be installed.
"c:\Program Files\CSV Converter\csvcnv.exe" c:\base\*.csv c:\exports\ /TOXLSX /SRCHDRSoftInterface's "Convert XLS" can use Excel (but is not required) and is more expensive ($500+), but supports more formats and has more options.
"c:\Program Files (x86)\Softinterface, Inc\Convert XLS\ConvertXLS.exe" /V /S"c:\base\*.csv" /T"c:\exports\*.xlsx" /F6 /C51 /M2"Gnumeric" is an open-source spreadsheet program that can perform a direct conversion, but it was discontinued for Windows in Aug 2014.
ssconvert file.csv file.xlsxIf you have Python installed, "csv2odf" is a open-source option and uses a templated approach to generate ods, odt, html, xlsx, or docx files.
csv2odf data.csv template.odt output.xlsx With Node 8+ and bash:
npm install -g pguardiario/csv2xlsx
for file in *.csv; do csv2xlsx "$file"; done The Easy way: open your csv file from Microsoft Excel, convert text to columns (select the cells/text, click Menu - Data - Text to Columns) set your option to convert.