3
# PyODConverter (Python OpenDocument Converter) v1.0.0 - 2008-05-05
5
# This script converts a document from one office format to another by
6
# connecting to an OpenOffice.org instance via Python-UNO bridge.
8
# Copyright (C) 2008 Mirko Nasato <mirko@artofsolving.com>
9
# Matthew Holloway <matthew@holloway.co.nz>
10
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl-2.1.html
11
# - or any later version.
14
DEFAULT_OPENOFFICE_PORT = 8100
16
from os.path import abspath
17
from os.path import isfile
18
from os.path import splitext
20
from StringIO import StringIO
24
from com.sun.star.beans import PropertyValue
25
from com.sun.star.uno import Exception as UnoException
26
from com.sun.star.connection import NoConnectException
27
from com.sun.star.io import XOutputStream
28
from com.sun.star.io import IOException
30
class DocumentConversionException(Exception):
32
def __init__(self, message):
33
self.message = message
38
class OutputStreamWrapper(unohelper.Base, XOutputStream):
39
""" Minimal Implementation of XOutputStream """
40
def __init__(self, debug=True):
42
self.data = StringIO()
45
sys.stderr.write("__init__ OutputStreamWrapper.\n")
47
def writeBytes(self, bytes):
49
sys.stderr.write("writeBytes %i bytes.\n" % len(bytes.value))
50
self.data.write(bytes.value)
51
self.position += len(bytes.value)
55
sys.stderr.write("Closing output. %i bytes written.\n" % self.position)
60
sys.stderr.write("Flushing output.\n")
62
def closeOutput(self):
64
sys.stderr.write("Closing output.\n")
67
class DocumentConverter:
69
def __init__(self, host='localhost',port=DEFAULT_OPENOFFICE_PORT):
70
self.localContext = uno.getComponentContext()
71
self.serviceManager = self.localContext.ServiceManager
72
resolver = self.serviceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", self.localContext)
74
context = resolver.resolve("uno:socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (host, port))
75
except NoConnectException, exception:
76
raise DocumentConversionException, "Failed to connect to OpenOffice.org on port %s. %s" % (port, exception)
77
self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
79
def convertByStream(self, stdinBytes):
80
inputStream = self.serviceManager.createInstanceWithContext("com.sun.star.io.SequenceInputStream", self.localContext)
81
inputStream.initialize((uno.ByteSequence(stdinBytes),))
83
document = self.desktop.loadComponentFromURL('private:stream', "_blank", 0, self._toProperties(
84
InputStream = inputStream
88
raise DocumentConversionException, "Error making document"
91
except AttributeError, e:
93
outputStream = OutputStreamWrapper(False)
95
document.storeToURL('private:stream', self._toProperties(
96
OutputStream = outputStream,
97
FilterName = "writer_pdf_Export"))
98
except IOException, e:
99
print ("IOException during conversion: %s - %s" % (e.ErrCode, e.Message))
102
openDocumentBytes = outputStream.data.getvalue()
104
return openDocumentBytes
107
def convertByPath(self, inputFile, outputFile):
108
inputUrl = self._toFileUrl(inputFile)
109
outputUrl = self._toFileUrl(outputFile)
112
document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(Hidden=True))
115
except AttributeError:
118
document.storeToURL(outputUrl, self._toProperties(FilterName="writer_pdf_Export"))
122
def _toFileUrl(self, path):
123
return uno.systemPathToFileUrl(abspath(path))
125
def _toProperties(self, **args):
128
prop = PropertyValue()
130
prop.Value = args[key]