3

I have a folder of .doc files I would like to convert to .txt format. How can I do that using LibreOffice's command line mode in Windows 7? The files are located in C:\Temp\Test.

kevinaskevin
  • 299
  • 3
  • 20

3 Answers3

6

Here is how I handled this task using Windows PowerShell

Note: before using LibreOffice from the command line you need to close all existing instances of Libreoffice. This means closing all GUI sessions of LibreOffice as well as inspecting TaskManager for soffice.exe or a LibreOffice process running the background.

One Item:

PS &("C:\Program Files (x86)\LibreOffice 4\program\soffice.exe") -headless -convert-to txt:Text -outdir C:\Temp C:\Temp\test\sample.doc

This created a file sample.txt in C:\Temp from the document sample.doc

Multiple Items:

foreach ($file in Get-ChildItem C:\Temp\test) 
{
    &("C:\Program Files (x86)\LibreOffice 4\program\soffice.exe") -headless -convert-to txt:Text -outdir C:\Temp C:\Temp\test\$file | Out-Null
}

This created a .txt file for every file in the folder C:\Temp\test

Again: Use task manager to ensure that a previous version of soffice.exe is not running. This means closing existing GUI versions of LibreOffice.

Explanation:

  • Here is the documentation regarding Starting LibreOffice Software With Parameters. This will explain the soffice.exe command executed above.
  • Headless mode starts the LibreOffice software without a GUI. What I refer to in the question as 'command line mode'.
  • -convert-to is an important parameter in this example. When using -convert-to you need to know what the output_filter_name is (Text in the example above). A reference for those names can be found here. The output_filter_name will be the name of the files in that list that have the suffix .xcu
    • For example, if I wanted to convert my .doc files to .pdf I would use the parameter -convert-to pdf:writer_pdf_Export (untested)
  • Here is a reference I used when answering this question.
  • For some reason .exe processes need to pipe to Out-Null to avoid overlapping one another. Go figure.
Community
  • 1
  • 1
kevinaskevin
  • 299
  • 3
  • 20
  • 1
    I just looked at this answer for my own reference after almost pulling my hair out. **Remember: Use task manager to ensure that a previous version of soffice.exe is not running. This means closing existing GUI versions of LibreOffice.** – kevinaskevin Jul 30 '14 at 13:27
2

The solution above was close, but required some alteration on LibreOffice 4.2 / Linux:

soffice --headless --convert-to txt:Text /path_to/file_to_convert.odt

(I did it with odt, the example I followed used doc: http://ask.libreoffice.org/en/question/14130/how-do-i-install-filters-for-the-soffice-command/)

markling
  • 1,232
  • 1
  • 15
  • 28
  • In Linux do you have to close all existing process of LibreOffice before you run in in headless (command-line) mode? – kevinaskevin May 27 '16 at 18:31
  • Its too long ago now to remember precisely, but there was some sort of issue with existing processes. It might have even been simply (or also) that LibreOffice clashed with any outside process that attempted to access a file it had already had open in the GUI. (doh!) – markling Oct 20 '16 at 19:24
2

An additional and important thing to add to @kevinaskevin 's answer is - the workaround is:

From the devs via IRC:

LO's "user installation" (i.e., ~/config/libreoffice) isn't designed to be accessed simultaneously by multiple processes; so when one soffice.bin is already running for a specific user installation, additional soffice.bin just forward their cmd line args to the first one and terminate immediately again (i.e., they shouldn't "fail" in the sense of exiting with a non-zero exit value)

if you want an independent headless LO, you can start it with an own user installation, -env:UserInstallation=<file URL to dir>

kwoxer
  • 3,734
  • 4
  • 40
  • 70
clockwerx
  • 29
  • 1