How do I convert .doc files to .txt using LibreOffice from the command line?
doc, libreoffice, powershell, windows
Solution
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.
Problem
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`.