]> git.lyx.org Git - lyx.git/blob - lib/scripts/libreoffice2eps.py
c7329c261eb02e1e05b3cb9f2329c85cd0168278
[lyx.git] / lib / scripts / libreoffice2eps.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file libreoffice2eps.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # \author Tommaso Cucinotta
9 #
10 # Full author contact details are available in file CREDITS
11
12
13 # This script converts an OpenOffice drawing to EPS.
14
15 # Usage:
16 #   python libreoffice2eps.py input.odg output.eps
17
18 import os, sys, tempfile, shutil
19
20 def runCommand(cmd):
21     ''' Utility function:
22         run a command, quit if fails
23     '''
24     if os.system(cmd) != 0:
25         print "Command '%s' failed." % cmd
26         sys.exit(1)
27
28 # We expect two args, the names of the input and output files.
29 if len(sys.argv) != 3:
30     sys.exit(1)
31
32 input, output = sys.argv[1:]
33
34 # Fail silently if the file doesn't exist
35 if not os.path.isfile(input):
36     sys.exit(0)
37
38 tmpdir = tempfile.mkdtemp()
39 fname = os.path.splitext(os.path.basename(input))[0]
40
41 # Generate the EPS file
42 runCommand('libreoffice -nologo -headless -convert-to eps -outdir "%s" "%s"' % (tmpdir, input))
43 shutil.move('%s/%s.eps' % (tmpdir, fname), '%s/%s.ps' % (tmpdir, fname))
44 runCommand('ps2eps "%s/%s.ps"' % (tmpdir, fname))
45 shutil.move('%s/%s.eps' % (tmpdir, fname), output)
46 os.remove('%s/%s.ps' % (tmpdir, fname))
47 os.rmdir(tmpdir)