user2307683
user2307683

Reputation: 2029

How to open file in Libre Office and save this like .doc file?

How to open file in Libre Office and save this like .doc file? It is possible? (create script for this)

Upvotes: 3

Views: 4986

Answers (2)

RafiK
RafiK

Reputation: 300

On win7, using LO 4.1 I had to do the following (from command line, you probably need to convert %f to %%f if running in a cmd script):

set path=%path%;C:\Program Files (x86)\LibreOffice 4\program
for %f in (*.odt) do (
    soffice.exe --headless --convert-to doc --outdir "C:\tmp" %f
)

Notes:

  • it will NOT work if any instance of LO is open!
  • outdir IS required
  • wildcards for input files are NOT supported (hence the for loop)

An according python script could look like this:

import os
import subprocess as sp

lo = r'C:\Program Files (x86)\LibreOffice 4\program\soffice.exe'

args = '--headless --convert-to doc --outdir "%(out)s" "%(inp)s"'

inp_path = './odt'
out_path = './doc'

inp_path = os.path.normpath(os.path.abspath(inp_path))
out_path = os.path.normpath(os.path.abspath(out_path))

for root, dirs, files in os.walk(inp_path):
  for fname in files:
    if fname.endswith('.odt'):
      i = os.path.join(inp_path,fname)
      sp.call(lo + ' ' + args%{'out': out_path, 'inp': i})

(copied and modified my answer from ask.libreoffice, also posted on superuser)

Upvotes: 0

Vyktor
Vyktor

Reputation: 21007

According to libreoffice manual (as a command line utility) you don't need python for this, but libreoffice should directly support this:

--convert-to output_file_extension[:output_filter_name] [--outdir output_dir] file... Batch converts files. If --outdir is not specified then the current working directory is used as the output directory for the convertedfiles.

Examples:

    --convert-to pdf *.doc

Converts all .doc files to PDFs.

    --convert-to pdf:writer_pdf_Export --outdir /home/user *.doc

Converts all .doc files to PDFs using the settings in the Writer PDF export dialog and saving them in /home/user.

Id you need to process many files, you could write simple bash script like this:

for i in `find folder -type f -name *.lwp` ; do
    libreoffice --headless --convert-to doc:"MS Word 2003 XML" $i
done

More detail instructions on how to invoke this command here or in manual specified earlier.

And you can basically do the same invocation from python and subprocess:

import os
import os.path
import subprocess

for i in os.listdir( SOURCE_FOLDER):
    if not i.endswith( '.lwp'):
        continue

    path = os.path.join( SOURCE_FOLDER, i)
    args = ['libreoffice', '--headless', '--convert-to',
            'doc:"MS Word 2003 XML"', path]

    subprocess.call(args, shell=False)

Upvotes: 4

Related Questions