def convert_by_stream(self, data, format=LIBREOFFICE_OPEN_DOCUMENT): data.seek(0) input_stream = self._service_manager.createInstanceWithContext( "com.sun.star.io.SequenceInputStream", self._local_context) input_stream.initialize((uno.ByteSequence(data.read()), )) document = self._desktop.loadComponentFromURL( 'private:stream', "_blank", 0, self._to_properties(InputStream=input_stream, ReadOnly=True)) if not document: raise Exception, "Error making document" try: document.refresh() except AttributeError: pass output_stream = output_stream_wrapper() try: document.storeToURL( 'private:stream', self._to_properties(OutputStream=output_stream, FilterName=format)) finally: document.close(True) if format == LIBREOFFICE_OPEN_DOCUMENT: doc_type = document_type.detect_document_type(output_stream.data) if doc_type != document_type.types.oasis_open_document: raise docvert_exception.converter_unable_to_generate_open_document( ) return output_stream.data
def convert_by_stream(self, data, format=LIBREOFFICE_OPEN_DOCUMENT): data.seek(0) input_stream = self._service_manager.createInstanceWithContext("com.sun.star.io.SequenceInputStream", self._local_context) input_stream.initialize((uno.ByteSequence(data.read()),)) document = self._desktop.loadComponentFromURL('private:stream', "_blank", 0, self._to_properties(InputStream=input_stream,ReadOnly=True)) if not document: raise Exception, "Error making document" try: document.refresh() except AttributeError: pass output_stream = output_stream_wrapper() try: document.storeToURL('private:stream', self._to_properties( OutputStream=output_stream, FilterName=format)) finally: document.close(True) if format == LIBREOFFICE_OPEN_DOCUMENT: doc_type = document_type.detect_document_type(output_stream.data) if doc_type != document_type.types.oasis_open_document: raise docvert_exception.converter_unable_to_generate_open_document() return output_stream.data
try: document.refresh() except AttributeError: pass output_stream = output_stream_wrapper() try: document.storeToURL('private:stream', self._to_properties(OutputStream=output_stream, FilterName=format)) except Exception, e: #ignore any error, verify the output before complaining pass finally: document.close(True) if format == LIBREOFFICE_OPEN_DOCUMENT or format == LIBREOFFICE_PDF: doc_type = document_type.detect_document_type(output_stream.data) output_stream.data.seek(0) if format == LIBREOFFICE_OPEN_DOCUMENT and doc_type != document_type.types.oasis_open_document: raise docvert_exception.converter_unable_to_generate_open_document("Unable to generate OpenDocument, was detected as %s. First 2 bytes = %s" % (doc_type, output_stream.data.read(2))) elif format == LIBREOFFICE_PDF and doc_type != document_type.types.pdf: raise docvert_exception.converter_unable_to_generate_pdf("Unable to generate PDF, was detected as %s. First 4 bytes = %s" % (doc_type, output_stream.data.read(4))) return output_stream.data def _to_properties(self, **args): props = [] for key in args: prop = PropertyValue() prop.Name = key prop.Value = args[key] props.append(prop) return tuple(props) def get_client(): global client
output_stream = output_stream_wrapper() try: document.storeToURL( 'private:stream', self._to_properties(OutputStream=output_stream, FilterName=format)) except Exception, e: #ignore any error, verify the output before complaining pass finally: document.close(True) if format == LIBREOFFICE_OPEN_DOCUMENT or format == LIBREOFFICE_PDF: doc_type = document_type.detect_document_type(output_stream.data) output_stream.data.seek(0) if format == LIBREOFFICE_OPEN_DOCUMENT and doc_type != document_type.types.oasis_open_document: raise docvert_exception.converter_unable_to_generate_open_document( "Unable to generate OpenDocument, was detected as %s. First 2 bytes = %s" % (doc_type, output_stream.data.read(2))) elif format == LIBREOFFICE_PDF and doc_type != document_type.types.pdf: raise docvert_exception.converter_unable_to_generate_pdf( "Unable to generate PDF, was detected as %s. First 4 bytes = %s" % (doc_type, output_stream.data.read(4))) return output_stream.data def _to_properties(self, **args): props = [] for key in args: prop = PropertyValue() prop.Name = key prop.Value = args[key] props.append(prop) return tuple(props)
try: document.refresh() except AttributeError: pass output_stream = output_stream_wrapper() try: document.storeToURL('private:stream', self._to_properties(OutputStream=output_stream, FilterName=format)) except Exception, e: #ignore any error, verify the output before complaining pass finally: document.close(True) if format == LIBREOFFICE_OPEN_DOCUMENT or format == LIBREOFFICE_PDF: doc_type = document_type.detect_document_type(output_stream.data) output_stream.data.seek(0) if format == LIBREOFFICE_OPEN_DOCUMENT and doc_type != document_type.types.oasis_open_document: raise docvert_exception.converter_unable_to_generate_open_document("Unable to generate OpenDocument, was detected as %s.\n\nAre you sure you tried to convert an office document? If so then it\nmight be a bug, so please contact http://docvert.org and we'll see\nif we can fix it. Thanks!" % doc_type) elif format == LIBREOFFICE_PDF and doc_type != document_type.types.pdf: raise docvert_exception.converter_unable_to_generate_pdf("Unable to generate PDF, was detected as %s. First 4 bytes = %s" % (doc_type, output_stream.data.read(4))) return output_stream.data def _to_properties(self, **args): props = [] for key in args: prop = PropertyValue() prop.Name = key prop.Value = args[key] props.append(prop) return tuple(props) def checkLibreOfficeStatus(): try: