Using the --convert-to command-line function to convert formats under Windows 10

    From The Document Foundation Wiki
    < Faq‎ | General

    Note

    We will consider the .docx to .odt conversion, but the syntax and steps are the same for other formats supported by LibreOffice.


    Context

    On Windows 10 we can use a command like soffice.exe --convert-to odt my_document.docx in order to convert my_document.docx from the MS docx format to the open document format (odt). The --convert-to function allows us to specify a set of documents by using the general wildcard syntax for files. For example, it is legal to use *.docx when you have a bunch of .docx files to convert from the current directory.


    Problem

    But when used in the Windows Command shell, it does not work at all. It seems that all the conversions are started simultaneously, and since two instances of LibreOffice (soffice.exe) cannot run at the same time when launched from the command line, the process will never end. At best, you will end with one or two files converted, but in any case, you will have to kill manually a bunch of LibreOffice processes hung in the system (twice as much as the number of files found, at first sight).


    Solution

    To overcome this, a simple way is to use the Windows Subsystem for Linux (WSL) environment provided by Microsoft for its version 10 of Windows. You may have to install it first, which is quite straightforward if you follow the WSL documentation.


    Once done, you will have access to a nice Bash shell running on top of the base Windows-system, and the --convert-to function will work properly, in a synchronous mode.


    Short story

    1. Identify the path to soffice.exe.
    2. cd to the directory containing the files to convert, after having created a destination directory.
    3. Give this command : <path to soffice.exe> --convert-to odt --outdir <my_output_directory> *.docx


    Example

    "/mnt/c/Program Files/LibreOffice 5/program/soffice.exe" --convert-to odt --outdir ./Converted_To_odt *.docx will convert the docx files from the current directory to their equivalent into the ./Converted_To_odt subdirectory.


    Why this syntax?

    The WSL mounts a complete file system on its own. So, the syntax to specify a file location in the directory tree is different from the Windows one. The first difference is the root point : Under Windows it uses to be C:\, the equivalent in the bash shell is /mnt/c/. Than, the token separator under Windows is \, in the bash it is the / Unix one. For the rest, the rules are the same, the bash shell supports the same character set as the Windows command shell as well as long filenames and filenames with spaces, provided that they are delimited by double-quotes (").


    How to write the command?

    The easiest way is to examine the shortcut you use every time you start LibreOffice. Right-click on it, than "Properties", than copy the "Target" field content. In the example given, it will be "C:\Program Files\LibreOffice 5\program\soffice.exe". Then adjust the syntax to the bash requirements as described before, to get this : "/mnt/c/Program Files/LibreOffice 5/program/soffice.exe" Beware, Unix is case-sensitive, Windows is not.


    For the rest, you are done.


    Conclusion

    Using the Bash shell on Windows is an easy way to convert a bunch of files from a format to another using a LibreOffice built-in command-line function. It works quietly.


    Or the simplest way, interactive

    It is even simpler to use the built-in LibreOffice Document converter Assistant.


    You can start it by navigating the menus: File ▸ Wizards ▸ Document Converter


    Note that a user has previously reported that he had to divide his files into bunches of 200 because the process sometimes crashed.

    The long description of the problem

    The problem is not that "all the conversions are started simultaneously". It's simply that LibreOffice's --convert-to in fact does not support using wildcards itself, and only works on non-Windows systems because they process the wildcards in their shells (and of course, these shells also work on Windows, like with Cygwin or WSL).

    What does this mean? This means that in Bash, when you send a command like soffice --convert-to odt path/to/*.doc, the command is first analyzed by Bash, and it finds the path/to/*.doc there, and enumerates every file that matches the mask, puts their names into one string, and substitutes the path/to/*.doc with something like path/to/1.doc path/to/2.doc path/to/345.doc. This is what LibreOffice sees.

    Windows' cmd.exe does not do that, and sends the path/to/*.doc to LibreOffice unmodified. LibreOffice simply tries to open the file named *.doc, and fails. That's all.

    In Windows' cmd.exe, the pre-processing should be done explicitly, like this:

      for %f in ("path\to\*.doc") do "C:\Program Files\LibreOffice\program\soffice" --convert-to odt "%f"
    

    The enhancement request to implement own wildcard processing is tdf#48413.