def __init__(self, port=DEFAULT_OPENOFFICE_PORT): self.parser = OOoDocumentParser() self.vnConverter = VietnameseTextConverter(decoderPrefix='internal_') self.oVnConverter = OOoVietnameseTextConverter(self.vnConverter) self.parser.setTextPortionConverter(self.oVnConverter) localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext) try: context = resolver.resolve( "uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port) except NoConnectException: raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port self.desktop = context.ServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", context) self._setupLogging()
def __init__(self, port=DEFAULT_OPENOFFICE_PORT): self.parser = OOoDocumentParser() self.vnConverter = VietnameseTextConverter(decoderPrefix='internal_') self.oVnConverter = OOoVietnameseTextConverter(self.vnConverter) self.parser.setTextPortionConverter(self.oVnConverter) localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext) try: context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port) except NoConnectException: raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context) self._setupLogging()
class DocumentConverter: def __init__(self, port=DEFAULT_OPENOFFICE_PORT): self.parser = OOoDocumentParser() self.vnConverter = VietnameseTextConverter(decoderPrefix='internal_') self.oVnConverter = OOoVietnameseTextConverter(self.vnConverter) self.parser.setTextPortionConverter(self.oVnConverter) localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext) try: context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port) except NoConnectException: raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context) self._setupLogging() def _setupLogging(self): filename = './converter.log' old_umask = os.umask(0077) # TODO: check if it fails and take countermesure in this case loglevel = logging.DEBUG # or logging.INFO logging.root.setLevel(loglevel) logging.basicConfig( format='%(asctime)s %(levelname)s %(message)s', filename=os.path.expanduser(filename), filemode='w') os.umask(old_umask) logging.info("B2UConverter loaded (Python %s)", sys.version) def _error_message(self, error_count): if error_count > 1: message = "with %s errors" % error_count elif error_count > 0: message = "with %s error" % error_count else: message = "without error" return message def convert(self, inputFile, outputFile): inputUrl = self._toFileUrl(inputFile) outputUrl = self._toFileUrl(outputFile) loadProperties = { "Hidden": True } inputExt = self._getFileExt(inputFile) if IMPORT_FILTER_MAP.has_key(inputExt): loadProperties.update(IMPORT_FILTER_MAP[inputExt]) document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(loadProperties)) try: document.refresh() except AttributeError: pass self.parser.processDocument(document) errmsg = self._error_message(self.parser.stats['errors']) logging.info("Conversion completed (%s).", errmsg) family = self._detectFamily(document) self._overridePageStyleProperties(document, family) outputExt = self._getFileExt(outputFile) storeProperties = self._getStoreProperties(document, outputExt) try: document.storeToURL(outputUrl, self._toProperties(storeProperties)) finally: document.close(True) def _overridePageStyleProperties(self, document, family): if PAGE_STYLE_OVERRIDE_PROPERTIES.has_key(family): properties = PAGE_STYLE_OVERRIDE_PROPERTIES[family] pageStyles = document.getStyleFamilies().getByName('PageStyles') for styleName in pageStyles.getElementNames(): pageStyle = pageStyles.getByName(styleName) for name, value in properties.items(): pageStyle.setPropertyValue(name, value) def _getStoreProperties(self, document, outputExt): family = self._detectFamily(document) try: propertiesByFamily = EXPORT_FILTER_MAP[outputExt] except KeyError: raise DocumentConversionException, "unknown output format: '%s'" % outputExt try: return propertiesByFamily[family] except KeyError: raise DocumentConversionException, "unsupported conversion: from '%s' to '%s'" % (family, outputExt) def _detectFamily(self, document): if document.supportsService("com.sun.star.text.WebDocument"): return FAMILY_WEB if document.supportsService("com.sun.star.text.GenericTextDocument"): # must be TextDocument or GlobalDocument return FAMILY_TEXT if document.supportsService("com.sun.star.sheet.SpreadsheetDocument"): return FAMILY_SPREADSHEET if document.supportsService("com.sun.star.presentation.PresentationDocument"): return FAMILY_PRESENTATION if document.supportsService("com.sun.star.drawing.DrawingDocument"): return FAMILY_DRAWING raise DocumentConversionException, "unknown document family: %s" % document def _getFileExt(self, path): ext = splitext(path)[1] if ext is not None: return ext[1:].lower() def _toFileUrl(self, path): return uno.systemPathToFileUrl(abspath(path)) def _toProperties(self, dict): props = [] for key in dict: prop = PropertyValue() prop.Name = key prop.Value = dict[key] props.append(prop) return tuple(props)
class DocumentConverter: def __init__(self, port=DEFAULT_OPENOFFICE_PORT): self.parser = OOoDocumentParser() self.vnConverter = VietnameseTextConverter(decoderPrefix='internal_') self.oVnConverter = OOoVietnameseTextConverter(self.vnConverter) self.parser.setTextPortionConverter(self.oVnConverter) localContext = uno.getComponentContext() resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext) try: context = resolver.resolve( "uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port) except NoConnectException: raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port self.desktop = context.ServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", context) self._setupLogging() def _setupLogging(self): filename = './converter.log' old_umask = os.umask(0077) # TODO: check if it fails and take countermesure in this case loglevel = logging.DEBUG # or logging.INFO logging.root.setLevel(loglevel) logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', filename=os.path.expanduser(filename), filemode='w') os.umask(old_umask) logging.info("B2UConverter loaded (Python %s)", sys.version) def _error_message(self, error_count): if error_count > 1: message = "with %s errors" % error_count elif error_count > 0: message = "with %s error" % error_count else: message = "without error" return message def convert(self, inputFile, outputFile): inputUrl = self._toFileUrl(inputFile) outputUrl = self._toFileUrl(outputFile) loadProperties = {"Hidden": True} inputExt = self._getFileExt(inputFile) if IMPORT_FILTER_MAP.has_key(inputExt): loadProperties.update(IMPORT_FILTER_MAP[inputExt]) document = self.desktop.loadComponentFromURL( inputUrl, "_blank", 0, self._toProperties(loadProperties)) try: document.refresh() except AttributeError: pass self.parser.processDocument(document) errmsg = self._error_message(self.parser.stats['errors']) logging.info("Conversion completed (%s).", errmsg) family = self._detectFamily(document) self._overridePageStyleProperties(document, family) outputExt = self._getFileExt(outputFile) storeProperties = self._getStoreProperties(document, outputExt) try: document.storeToURL(outputUrl, self._toProperties(storeProperties)) finally: document.close(True) def _overridePageStyleProperties(self, document, family): if PAGE_STYLE_OVERRIDE_PROPERTIES.has_key(family): properties = PAGE_STYLE_OVERRIDE_PROPERTIES[family] pageStyles = document.getStyleFamilies().getByName('PageStyles') for styleName in pageStyles.getElementNames(): pageStyle = pageStyles.getByName(styleName) for name, value in properties.items(): pageStyle.setPropertyValue(name, value) def _getStoreProperties(self, document, outputExt): family = self._detectFamily(document) try: propertiesByFamily = EXPORT_FILTER_MAP[outputExt] except KeyError: raise DocumentConversionException, "unknown output format: '%s'" % outputExt try: return propertiesByFamily[family] except KeyError: raise DocumentConversionException, "unsupported conversion: from '%s' to '%s'" % ( family, outputExt) def _detectFamily(self, document): if document.supportsService("com.sun.star.text.WebDocument"): return FAMILY_WEB if document.supportsService("com.sun.star.text.GenericTextDocument"): # must be TextDocument or GlobalDocument return FAMILY_TEXT if document.supportsService("com.sun.star.sheet.SpreadsheetDocument"): return FAMILY_SPREADSHEET if document.supportsService( "com.sun.star.presentation.PresentationDocument"): return FAMILY_PRESENTATION if document.supportsService("com.sun.star.drawing.DrawingDocument"): return FAMILY_DRAWING raise DocumentConversionException, "unknown document family: %s" % document def _getFileExt(self, path): ext = splitext(path)[1] if ext is not None: return ext[1:].lower() def _toFileUrl(self, path): return uno.systemPathToFileUrl(abspath(path)) def _toProperties(self, dict): props = [] for key in dict: prop = PropertyValue() prop.Name = key prop.Value = dict[key] props.append(prop) return tuple(props)