Beispiel #1
0
def get_document(file_name):
    path = os.path.join(MEDIA_ROOT, "template_docs/" + file_name)
    debug(__name__, path)
    
    local = uno.getComponentContext()
    resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)

    document = None
    try:
        context = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
        document = desktop.loadComponentFromURL("file://" + path, "_blank", 0, ())
    except UnoException, e:
        error(__name__,"OpenOffice connection error: %s" % e.Message)
        return None
Beispiel #2
0
    path = os.path.join(MEDIA_ROOT, "template_docs/" + file_name)
    debug(__name__, path)
    
    local = uno.getComponentContext()
    resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)

    document = None
    try:
        context = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
        document = desktop.loadComponentFromURL("file://" + path, "_blank", 0, ())
    except UnoException, e:
        error(__name__,"OpenOffice connection error: %s" % e.Message)
        return None

    debug(__name__, "Connected to OpenOffice")
    return document

def replace(document, what, for_what):
  ReplaceDescr = document.createReplaceDescriptor()
  ReplaceDescr.SearchString = "$%s" % what
  ReplaceDescr.ReplaceString = for_what
  Found = document.replaceAll( ReplaceDescr )

def generate(document, values, save_to):
    for k in values.keys():
        replace(document, k, values[k])

    file_name = 'file://' + save_to
    if file_name[-4:] != '.odt':
        file_name += '.odt'