ThisCalculator

Everyday

Convert XLS to CSV in Command Line

Updated · Standard formula, shown below

Convert XLS to CSV in Command Line

You can convert an XLS file to CSV directly from the command line using a tool like ssconvert (from Gnumeric), libreoffice --headless, or a small Python script with the pandas library. The exact command depends on your operating system and whether the XLS file contains multiple sheets, but the core idea is the same: read the spreadsheet and write it as comma-separated values.

Why Convert XLS to CSV?

CSV (comma-separated values) is a plain-text format that almost any program can read. Unlike XLS, which is a proprietary binary format (or an XML-based one in newer versions), CSV contains no formatting, formulas, or macros—just the raw cell values separated by commas. This makes it ideal for:

  • Importing data into databases, data-analysis tools, or custom scripts.
  • Version control: CSV files diff cleanly, so you can track changes in a spreadsheet over time.
  • Automation: command-line tools can process CSV files easily, and you can chain conversions into larger pipelines.

If you only need to convert one file occasionally, opening it in a spreadsheet app and using “Save As� works fine. But if you need to convert many files, or you want to integrate the conversion into a script, the command line is the way to go.

Prerequisites: What You Need

Before you run any command, you need a tool that can read XLS files. Here are the most common options:

  • Gnumeric’s ssconvert – A lightweight spreadsheet converter that handles XLS, XLSX, ODS, and CSV. Available on Linux and macOS via package managers.
  • LibreOffice – A full office suite that can run headless (without a GUI) and convert many formats. Works on Windows, macOS, and Linux.
  • Python with pandas or xlrd – If you already use Python, this is a flexible option. Pandas can read XLS (via xlrd) and write CSV.
  • R with readxl – For R users, the readxl package reads XLS/XLSX and you can write CSV with base R.

Windows users don’t have a built-in command-line converter, but you can install LibreOffice or use Python. macOS users can install Gnumeric or LibreOffice via Homebrew.

Method 1: Using ssconvert (Gnumeric)

If you have Gnumeric installed, ssconvert is the simplest tool. The basic syntax is:

ssconvert input.xls output.csv

This converts the first sheet of the XLS file to CSV. If your file has multiple sheets and you want a specific one, you can specify it like this:

ssconvert input.xls output.csv --export-type=Gnumeric_stf:stf_csv

But that still exports only the first sheet. To export all sheets, you can use the --export-type=Gnumeric_stf:stf_csv with a placeholder for the sheet name:

ssconvert input.xls output-%n.csv

This creates one CSV file per sheet, with the sheet name replacing %n (e.g., output-Sheet1.csv).

Method 2: Using LibreOffice Headless

LibreOffice is more widely available and works on all major platforms. The command is:

libreoffice --headless --convert-to csv input.xls

This creates a file named input.csv in the same directory. To specify an output directory, add --outdir:

libreoffice --headless --convert-to csv --outdir /path/to/output input.xls

One important limitation: LibreOffice only converts the first sheet by default. If you need all sheets, you have to write a macro or use a different tool. Also, note that the CSV output uses the delimiter and encoding set in your LibreOffice settings—usually comma and UTF-8, but you can override with the --infilter option, though that gets complex.

Method 3: Using Python (pandas)

If you have Python installed, pandas makes it easy to convert with full control over sheets and encoding. First install pandas and xlrd:

pip install pandas xlrd

Then run a one-liner:

python -c "import pandas as pd; pd.read_excel('input.xls').to_csv('output.csv', index=False)"

This converts the first sheet. To convert a specific sheet, use sheet_name='Sheet2'. To convert all sheets, loop over them:

python -c "import pandas as pd; xl = pd.ExcelFile('input.xls'); [pd.read_excel(xl, sheet).to_csv(f'{sheet}.csv', index=False) for sheet in xl.sheet_names]"

Pandas also lets you change the delimiter (e.g., sep=';' for semicolon-separated values) and handle encoding issues with encoding='utf-8'.

Common Problems and How to Fix Them

Here are the most frequent issues you’ll encounter:

  • “No such file or directoryâ€� – Make sure you’re in the correct directory or use the full path to the file.
  • Multiple sheets – Most tools only export the first sheet by default. Use the sheet-specific methods above.
  • Delimiter issues – Some locales use semicolons instead of commas. Check the output file; if it’s semicolon-separated, you can change it with --infilter in LibreOffice or sep in pandas.
  • Encoding problems – If you see weird characters, try specifying UTF-8 encoding (e.g., --convert-to csv:Text - txt - csv (StarCalc):44,34,76 in LibreOffice, or encoding='utf-8' in pandas).
  • Formulas not calculated – CSV stores values, not formulas. If your XLS has formulas, the converter will output the last calculated value, which may be stale if the file was never opened in a spreadsheet app.

Comparing the Tools

Which tool should you use? Here’s a quick comparison:

  • ssconvert – Fast, lightweight, and handles multiple sheets well. But it’s not installed by default on most systems, and the syntax for multiple sheets is a bit obscure.
  • LibreOffice – Available everywhere, but slower to start and only converts the first sheet without extra setup.
  • Python/pandas – Most flexible: you can choose sheets, change delimiters, handle encoding, and even transform data during conversion. But it requires Python and the pandas library.

For a one-off conversion, LibreOffice is the easiest to install. For scripting and automation, pandas is the most powerful. If you’re on Linux and want speed, ssconvert is great.

Frequently Asked Questions

How do I convert an XLSX file instead of XLS?

The same commands work for XLSX files. ssconvert, LibreOffice, and pandas all read both formats. Just change the input file extension.

Can I convert a specific sheet from an XLS file?

Yes. With ssconvert, use ssconvert input.xls output.csv --export-type=Gnumeric_stf:stf_csv --sheet 'Sheet2' (check the exact option syntax for your version). With LibreOffice, you can’t directly; use a macro or convert to ODS first. With pandas, use the sheet_name parameter.

Why is my CSV file full of semicolons instead of commas?

This happens when your system’s locale uses semicolons as the default delimiter (common in European locales). In LibreOffice, you can override with the --infilter option, but it’s easier to use pandas and specify sep=','.

Will the CSV file contain the formulas or just the values?

Just the values. CSV is a plain-text format and cannot store formulas. The converter reads the current value of each cell (the result of the formula) and writes that.

Can I convert multiple XLS files at once?

Yes. In bash, you can loop: for f in *.xls; do libreoffice --headless --convert-to csv "$f"; done. In Python, you can use a for loop over the files.

Is there a way to convert without installing anything?

If you have a modern browser, you can use an online converter, but that requires uploading your file, which may be a privacy concern. For sensitive data, it’s better to use a local tool.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.