Exemple #1
0
 def getOOoInstance(self, headless):
     # Just convert the document?
     if not self._instance:
         self._instance = OpenOfficeInstance(headless=True)
     self._headless = headless
     ctx = self._instance.getContext()
     self._smgr = ctx.ServiceManager
     self._desktop = self._smgr.createInstanceWithContext(
         'com.sun.star.frame.Desktop', ctx)
Exemple #2
0
 def getOOoInstance(self, headless):
    # Just convert the document?
    if not self._instance:
       self._instance = OpenOfficeInstance(headless = True)
    self._headless = headless
    ctx = self._instance.getContext() 
    self._smgr = ctx.ServiceManager
    self._desktop = self._smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
Exemple #3
0
class OpenOfficeBackend(object):
    def __init__(self):
        self._instance = None
        self._doc = None

    def getOOoInstance(self, headless):
        # Just convert the document?
        if not self._instance:
            self._instance = OpenOfficeInstance(headless=True)
        self._headless = headless
        ctx = self._instance.getContext()
        self._smgr = ctx.ServiceManager
        self._desktop = self._smgr.createInstanceWithContext(
            'com.sun.star.frame.Desktop', ctx)

    def _convertToURL(self, pathname):
        """Convert a Windows or Linux pathname into an OOo URL."""
        if len(pathname) > 1:
            if pathname[1:2] == ":":
                pathname = "/" + pathname[0] + "|" + pathname[2:]
        pathname = pathname.replace("\\", "/")
        pathname = "file://" + pathname
        return pathname

    def _isTextDoc(self, path):
        return path.endswith('.ott')

    def loadTemplate(self, templatePath):
        url = self._convertToURL(os.path.realpath(templatePath))
        args = (uno.createUnoStruct('com.sun.star.beans.PropertyValue'), )
        args[0].Name = 'Hidden'
        args[0].Value = True
        if self._headless:
            self._doc = self._desktop.loadComponentFromURL(
                url, '_blank', 0, args)
        else:
            self._doc = self._desktop.loadComponentFromURL(
                url, '_blank', 0, ())

        if self._isTextDoc(templatePath):
            self._textCursor = self._doc.Text.createTextCursor()
        else:
            self._textCursor = None
        self._listener = myCloseListener(self._doc)
        self._doc.addCloseListener(self._listener)

    def renderWith(self, renderer, content, *args, **kw):
        renderer.init(self._doc, self._textCursor)
        renderer.renderJson(content, *args, **kw)

    def saveAs(self, outFile):
        def createProps(**args):
            props = []
            for key in args:
                prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
                prop.Name = key
                prop.Value = args[key]
                props.append(prop)
            return tuple(props)

        # See: https://github.com/LibreOffice/core/blob/master/filter/source/pdf/pdfexport.cxx#L444
        if outFile.upper().endswith('.PDF'):
            settings = createProps(
                ExportBookmarks=True,
                ExportNotes=True,
            )

            args = createProps(FilterName="writer_pdf_Export",
                               FilterData=uno.Any(
                                   "[]com.sun.star.beans.PropertyValue",
                                   settings))
        elif outFile.upper().endswith('.ODT'):
            args = ()
        elif outFile.upper().endswith('.OTS'):
            args = ()
        elif outFile.upper().endswith('.DOCX'):
            args = ()
        else:
            raise RuntimeError('Unknown output file format.')

        self._doc.storeToURL(self._convertToURL(os.path.realpath(outFile)),
                             args)

    def close(self):
        if self._doc:
            self._doc.dispose()
        else:
            return
        # Wait for the document to be closed
        while self._listener._alive:
            time.sleep(0.1)
        self._instance.close()
Exemple #4
0
class OpenOfficeBackend(object):
   def __init__(self):
      self._instance = None
      self._doc = None

   def getOOoInstance(self, headless):
      # Just convert the document?
      if not self._instance:
         self._instance = OpenOfficeInstance(headless = True)
      self._headless = headless
      ctx = self._instance.getContext() 
      self._smgr = ctx.ServiceManager
      self._desktop = self._smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
   
   def _convertToURL(self, pathname):
       """Convert a Windows or Linux pathname into an OOo URL."""
       if len(pathname) > 1:
           if pathname[1:2] == ":":
               pathname = "/" + pathname[0] + "|" + pathname[2:]
       pathname = pathname.replace("\\", "/")
       pathname = "file://" + pathname
       return pathname 

   def _isTextDoc(self, path):
      return path.endswith('.ott')

   def loadTemplate(self, templatePath):
      url = self._convertToURL(os.path.realpath(templatePath))
      args = (uno.createUnoStruct('com.sun.star.beans.PropertyValue'),)
      args[0].Name  = 'Hidden'
      args[0].Value = True
      if self._headless:
         self._doc = self._desktop.loadComponentFromURL(url, '_blank', 0, args)
      else:
         self._doc = self._desktop.loadComponentFromURL(url, '_blank', 0, ())
      
      if self._isTextDoc(templatePath):
         self._textCursor = self._doc.Text.createTextCursor()
      else:
         self._textCursor = None
      self._listener = myCloseListener(self._doc)
      self._doc.addCloseListener(self._listener)

   def renderWith(self, renderer, content, *args, **kw):
      renderer.init(self._doc, self._textCursor)
      renderer.renderJson(content, *args, **kw)

   def saveAs(self, outFile):
      if outFile.upper().endswith('.PDF'):
         args = (uno.createUnoStruct('com.sun.star.beans.PropertyValue'),)
         args[0].Name  = 'FilterName'
         args[0].Value = 'writer_pdf_Export'
      elif outFile.upper().endswith('.ODT'):
         args = ()
      elif outFile.upper().endswith('.OTS'):
         args = ()
      else:
         raise RuntimeError('Unknown output file format.')
      self._doc.storeToURL(self._convertToURL(os.path.realpath(outFile)), args)

   def close(self):
      if self._doc:
         self._doc.dispose()
      else:
         return
      # Wait for the document to be closed
      while self._listener._alive:
         time.sleep(0.1)
      self._instance.close()