Convert ODS to CSV using command line when Libreoffice instance is running
Asked Answered
D

4

16

I'm working with an ODS file in LibreOffice, and every now and then I want to convert it to CSV from the command line. I have found two tools for this: libreoffice --headless --convert-to csv and unoconv -f csv but none of them works when there is an UI instance of LibreOffice running.

Is there a way to convert an ODS file to CSV from the command line, while LibreOffice UI instance is running?

Disparage answered 29/10, 2020 at 10:21 Comment(1)
@tohuwawohu No, I don't receive any error messages because both libreoffice --headless and unoconv are working as designed. They are not designed to work on the same time as LibreOffice UI is running.Disparage
L
13

To convert csv on the command line while LibreOffice is running, just skip the --headless parameter. The following command (run in PowerShell) worked for me even while C:\TEMP\Untitled1.ods resp. /tmp/Untitled1.ods was opened in LibreOffice Calc:

Windows 10 (tested with LibreOffice 7.0.1)

& 'C:\Program Files\LibreOffice\program\soffice.exe' --convert-to csv --outdir C:\TEMP\ .\Untitled1.ods

Or similar, if the present working directory is C:\Program Files\LibreOffice\program\:

.\soffice --convert-to csv --outdir C:\TEMP\ C:\TEMP\Untitled1.ods

Linux (tested with LibreOffice 6.4.6)

soffice --convert-to csv --outdir /tmp/ /tmp/Untitled1.ods

On linux, you could alternatively use the libreoffice command instead of soffice (seems to be linux-specific...):

libreoffice --convert-to csv --outdir /tmp/ /tmp/Untitled1.ods

In all cases, Untitled.csv was created in the expected place.

Lesko answered 29/10, 2020 at 19:28 Comment(8)
Ahh, the docs of the --headless parameter is: "Starts in "headless mode", which allows using the application without user a interface." (These are the exact words :) )Disparage
And even more interesting: The description of the --convert-to flag is: "(...) It implies --headless."Disparage
Note the & prefix on Windows if using powershell. This is not necessary if using the "old" Command Prompt.Rumanian
This will destroy any utf-8 characters unless you follow the directions here: help.libreoffice.org/latest/en-GB/text/shared/guide/…Filtrate
@SurpriseDog: Since the question didn't care about encoding parameters, my the answer didn't need to care about, too. BTW: Pointing to an external URL is discouraged since external content may change or disappear. Thus, feel free to embed the relevant information in your own answer.Lesko
@SurpriseDog: BTW, your comment is wrong. I've just tested the command i've proposed with an ods containing a selection of utf-8 characters starting from c486 (Ć, ć, Ĉ, ĉ, Ċ, ċ, Č and so on). The resulting CSV looks perfectly fine.Lesko
This is what I get without adding the special filtes duod�cimo This is what I get with it duodécimo - I'm glad that it works fine on your file, but I added my comment so that anyone else having this problem has a chance at fixing it. - Cheers!Filtrate
@Filtrate I've tested again, now with your string, and the resulting csv looks fine both in LO Cals as well as in Notepad++. If you encounter any encoding-specific problems, you should create a new question providing essential information (OS, LO version, i18n settings, application used to open the csv and so on) to reproduce the issue.Lesko
D
1

Tested with MacOS :

/Applications/LibreOffice.app/Contents/MacOS/soffice --convert-to csv myspread.ods 

This wrote out myspread.csv in the same directory

Divergence answered 27/11, 2022 at 15:5 Comment(0)
W
1

With more recent versions of LibreOffice, it is possible to generate CSV files on a per-sheet basis for all of the sheets in the ods document. This is possible with an additional token -- e.g., see https://git.libreoffice.org/core/+/b8903bc106dad036acb3d117e5c4fc955697fe02%5E%21/ . The sample usage in the commit message:

soffice --convert-to csv:"Text - txt - csv (StarCalc)":44,34,UTF8,1,,0,false,true,false,false,false,-1 sample.ods
Wb answered 22/1, 2024 at 5:47 Comment(0)
I
0

Try the ods2csv utility: https://hackage.haskell.org/package/ods2csv It simply scans the XML structure of the ODS file and extracts the displayed text of every cell. You also find it packaged in NixOS:

nix-shell -p haskellPackages.ods2csv
Iand answered 28/7, 2024 at 14:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.