How to convert XLSX to CSV from the command line
Batch-convert Excel files to CSV with LibreOffice, csvkit, PowerShell or ssconvert, including every sheet and UTF-8 output. Commands tested and explained.
Need to convert many Excel files, or run the conversion on a schedule or a server? These command-line tools do it without opening a spreadsheet app. All of them are free.
LibreOffice (Windows, Mac, Linux)
LibreOffice can convert from the command line without opening a window. The simplest form converts the first sheet of every .xlsx file in a folder:
soffice --headless --convert-to csv --outdir out *.xlsx
To control the format and export every sheet (LibreOffice 7.2 or later), add the CSV filter options:
soffice --headless --convert-to "csv:Text - txt - csv (StarCalc):44,34,76,1,,0,false,true,true,false,false,-1" --outdir out workbook.xlsx
44is the comma separator (use59for a semicolon or9for a tab).34puts double quotes around text that needs them, and76is UTF-8.- The
truein ninth place saves cell contents as shown, with number formats applied. - The final
-1exports every sheet, asworkbook-SheetName.csv. Use1,2and so on for a single sheet.
It works with .xls, .xlsb and .ods files too. On Windows, run it as "C:\Program Files\LibreOffice\program\soffice.exe". Close LibreOffice first, or the command may return without doing anything.
csvkit (Python)
csvkit is a set of command-line CSV tools. Install it with pip install csvkit, then:
in2csv -n workbook.xlsx # list the sheet names
in2csv -I --sheet "Orders" workbook.xlsx > orders.csv
in2csv -I --write-sheets - --use-sheet-names workbook.xlsx
The last line writes every sheet to its own CSV, named after the workbook and the sheet. Keep the -I (--no-inference) flag: without it, csvkit guesses types and turns 000451 into 451.
PowerShell with Excel installed (Windows)
If Excel is installed, PowerShell can drive it to save every sheet as a UTF-8 CSV. Change the two paths, then paste it into PowerShell:
$file = "C:\data\workbook.xlsx"
$out = "C:\data\csv"
New-Item -ItemType Directory -Force -Path $out | Out-Null
$excel = New-Object -ComObject Excel.Application
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open($file)
foreach ($ws in $wb.Worksheets) {
$ws.Copy()
$excel.ActiveWorkbook.SaveAs((Join-Path $out ($ws.Name + ".csv")), 62)
$excel.ActiveWorkbook.Close($false)
}
$wb.Close($false)
$excel.Quit()
The number 62 is Excel's code for CSV UTF-8 (Excel 2016 and later). Use 6 for the older ANSI CSV. Like the Excel macro, this saves values as Excel displays them.
Gnumeric's ssconvert (Linux)
On Linux, the Gnumeric spreadsheet's ssconvert tool is light and fast:
ssconvert -S --export-type=Gnumeric_stf:stf_csv workbook.xlsx "out_%s.csv"
-S writes one file per sheet, and %s is replaced by the sheet name (%n gives the sheet number).
Which one should you use?
| Tool | Needs | Values | Best for |
|---|---|---|---|
| LibreOffice | LibreOffice installed | As shown or raw | Batch jobs on any OS, all formats |
| csvkit | Python | Raw values | Data pipelines and quick checks |
| PowerShell | Windows with Excel | As shown | Office machines that already have Excel |
| ssconvert | Gnumeric | As shown | Lightweight Linux servers |
For a one-off conversion, xlsxtocsv.com is quicker: no install, every sheet in one click, and the file stays on your computer. For scripts, see converting with Python.
Skip the steps: convert every sheet to CSV in your browser, free.
Convert XLSX to CSV