2 # -*- coding: utf-8 -*-
4 This sample script opens a ODF file using OpenOffice.org and
8 # Copyright (c) 2008 by Hartmut Goebel <h.goebel@goebel-consult.de>
9 # Licenced under the GNU General Public License v3 (GPLv3)
10 # see file LICENSE-gpl-3.0.txt
12 # Based on some ideas from LX-office's 'oo-uno-convert-pdf.py'
13 # (see <http:///www.lx-office.org>).
15 # This is just a sample script for openoffice-python. If you are
16 # looking for a converter which supports more formats, you may have a
17 # look at <http://www.artofsolving.com/opensource/pyodconverter>
18 # (which does not use openoffiec-python).
21 import openoffice.interact
23 from com.sun.star.beans import PropertyValue
28 def write_pdf(doc, pdf_filename):
30 PropertyValue("FilterName", 0, "writer_pdf_Export", 0),
31 PropertyValue("Overwrite", 0, True, 0),
33 pdf_filename = os.path.expanduser(pdf_filename)
34 pdf_filename = os.path.abspath(pdf_filename)
35 pdf_url = unohelper.systemPathToFileUrl(pdf_filename)
36 print >> sys.stderr, pdf_url
37 doc.storeToURL(pdf_url, out_props)
40 def convert2pdf(odf_filename, pdf_filename=None, opts=None):
41 desktop = openoffice.interact.Desktop(host=opts.host, port=opts.port)
42 # If the file does not exist, this will fail with:
43 # openoffice.interact.IllegalArgumentException: URL seems to be
45 # (where the module name is missleading!)
46 doc = desktop.openFile(odf_filename, hidden=True)
48 pdf_filename = os.path.splitext(odf_filename)[0] + '.pdf'
49 write_pdf(doc, pdf_filename)
52 print >> sys.stderr, "Tear down is not yet implemented."
55 if __name__ == '__main__':
57 parser = optparse.OptionParser('%prog [options] ODF-Filename [PDF-Filename]')
58 parser.add_option('--tear-down', action='store_true',
59 help="tear down OOo after convertion")
60 group = parser.add_option_group('To connect to already running server use:')
61 group.add_option('--host', #default='localhost',
62 help="hostname/ip of server (default: %default)")
63 group.add_option('--port', default=2002, type=int,
64 help="port the server is listening on (default: %default)")
66 opts, args = parser.parse_args()
67 if len(args) == 0 or len(args) > 2:
68 parser.error('expects one or two arguments')
71 convert2pdf(*args, **{'opts': opts})