I noticed that LibreOffice Writer has a good quality: when loading a .docx file, and saving it right away, someone the saved file structure is a bit different than the original file - in a good way - it kinda like solve some .docx structure issues, etc (which allows me later to process it better with Apache POI).

I would like to simply use it to fix .docx files, by loading them and saving them right away - either from the command line, or somehow by invoking the Writer functions directly.

Would appreciate some pointers to the solution.

  • Yes, I just didn't find how to specify the output file (only the output dir)..
    – Nathan B
    May 26 '19 at 13:54

You can use the libreoffice CLI tool, it has a --convert-to option to convert files which can be used to convert to the same format as you want. If you use this without specifying an output directory it overwrites the original file though, and you can’t give it a different output filename. Work around that by converting the file to /tmp/ first and move it back afterwards:

libreoffice --convert-to docx --outdir /tmp/ NAME.DOCX && mv /tmp/NAME.DOCX ./NEWNAME.DOCX

You just need to change the bold all-caps parts, NAME.DOCX to your input filename and NEWNAME.DOCX to the desired output filename.

On the bash command line, you can shorten this to:

libreoffice --convert-to docx --outdir /tmp/ NAME.DOCX && mv !#:4!#:5 ./NEWNAME.!#:2

This uses bash History Expansion so that you don’t have to enter paths and filenames multiple times – you just need to change NAME.DOCX to your input filename and NEWNAME to the desired output filename without the extension.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.