19

I am using a couple of documentation files in LibreOffice (.odt) format. Normally, I access them with LibreOffice Writer of course. But there are times when I can only connect to the site with a text terminal.

I am wondering if there is a terminal-based tool that can show me the contents of these files in approximate correct format? (The files mainly contain simple text, bullet lists, and a few 1x1 tables, so it's relatively simple stuff in terms of formatting.)

P.S. This question is not about starting LibreOffice itself from the command line (which is anwered here).

rookie09
  • 293
  • 2
  • 5
  • 2
    Possible duplicate of [how to view a doc from command line?](https://askubuntu.com/questions/364872/how-to-view-a-doc-from-command-line) – pLumo Nov 13 '17 at 08:41
  • 3
    Not in the original answer, but you can use `odt2txt document.odt | less` to directly view the file. – pLumo Nov 13 '17 at 08:47
  • @RoVo That suits for an answer! It would be interesting whether `odt2txt` does a job as good as `libreoffice --convert-to` (which I'd expect to work *very* well without having tried it extensively). The latter (as far as I found out) is not able to write to stdout so that piping directly unfortunately isn't available for it. Caveat: If you just quickly want to view (or search!) the content of your document and maybe don't care too much about formatting, `odt2txt | …` is the way to go. Else let `libreoffice` convert your document as explained below. – dessert Nov 13 '17 at 12:25
  • 2
    Simple documents seem to be converted well with both tools. Unfortunately tables are not converted to ascii tables ... – pLumo Nov 13 '17 at 12:34

4 Answers4

23

libreoffice provides a --convert-to option which can be used to convert a document to e.g. text or html:

  • convert input.odt to input.txt:
    libreoffice --convert-to "txt:Text (encoded):UTF8" input.odt

  • convert every .odt in the current directory to .html:
    libreoffice --convert-to "html:XHTML Writer File:UTF8" *.odt

  • convert every .ods in the current directory to .csv:
    libreoffice --convert-to csv *.ods

The output can be opened with the pager or terminal browser of your liking: less, most or w3m to list just three.

dessert
  • 39,392
  • 12
  • 115
  • 163
  • Nice! Can you use this to display the txt version directly in terminal instead of writing to a file? – Puck Nov 13 '17 at 16:36
  • Unfortunately not, see [my comment above](https://askubuntu.com/questions/975937/tool-for-viewing-libreoffice-writer-files-in-terminal-window/975947?noredirect=1#comment1566018_975937). – dessert Nov 13 '17 at 16:43
21

There is a tool called odt2txt that can convert odt to txt.

Compared to libreoffice I can see two benefits:

  • Lightweight if you don't have libreoffice installed (e.g. on a server)
  • It can print to stdout for direct viewing of files.

Installation:

sudo apt install odt2txt

Then you can directly view an odt:

odt2txt document.odt | less
pLumo
  • 26,204
  • 2
  • 57
  • 87
  • If you want to convert to a file just redirect the output with `odt2txt document.odt > file` or use the `--output=file` option. – dessert Nov 13 '17 at 16:18
12

LibreOffice has a --cat option which exists in version 5.1 but not 4.2. Not sure exactly when it was introduced.

libreoffice --cat "Untitled 1.odt" --headless | less

For more information:

libreoffice --help
Jim K
  • 1,403
  • 10
  • 14
  • It's a shame this useful option is neither documented in `libreoffice`'s manpage nor on https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters, however at least `libreoffice -h` lists it. – dessert Nov 13 '17 at 21:59
  • @Ganton: Please add a comment instead of editing the answer. I tried your suggestion in version 7.5 but could not get it to work, so I did this instead: `--convert-to "txt:Text (encoded):UTF8"`. Documentation: https://help.libreoffice.org/latest/en-US/text/shared/guide/start_parameters.html. – Jim K Jun 10 '23 at 16:04
  • Beware that `libreoffice --cat` [silently does nothing if any LibreOffice is already open](https://ask.libreoffice.org/t/convert-to-command-line-parameter/840/10). – Ganton Jun 15 '23 at 17:34
  • @JimK: I added the comment, it's very important as it saves a lot of headaches if (for example) a LibreOffice Writer is being executed (because nothing happens, no error message is seen). The comment is about `libreoffice --cat`, there are no problems using `--convert-to` (as you have seen). About "*I tried your suggestion in version 7.5*", I added a warning, not a suggestion, could you clarify about that? – Ganton Jun 21 '23 at 09:40
  • @Ganton: `--cat` did not work using 7.5, and closing all LibreOffice processes first (your suggestion) did not fix the problem. – Jim K Jun 21 '23 at 17:30
  • @JimK: I've tried it again with Libreoffice 7.5.4.2 under Kubuntu 23.04, and with Kubuntu 22.04 and its Libreoffice (in a virtual machine), and the same happened: `libreoffice --cat FILE.odt` worked if (for example) `ps aux | grep [l]ibre` showed nothing. – Ganton Jun 23 '23 at 11:10
0

Another tool that you can use is unoconv which can convert "between any document format supported by LibreOffice/OpenOffice"

unoconv -f txt my.odt
phuclv
  • 606
  • 1
  • 8
  • 38