Search code examples
pythonopenoffice.orglibreofficeuno

Python storeAsUrl fails to save document


This code should open a template and save it using storeAsUrl. It opens my template but fails saving it. I have no idea why.

import pyuno

from os.path import realpath, join
from com.sun.star.beans import PropertyValue


SAVE_PATH = '/home/user/Bureau/'
FILENAME = 'P{0:04d}.doc'
TEMPLATE_PATH = '/home/user/Bureau/'
TEMPLATE_NAME = 'bal.ott'


def convert_path_to_url(path):
    return pyuno.systemPathToFileUrl(realpath(path))


def python_bal(*args):
    bal_doc = BALDocument(1)
    bal_doc.save()


class BALDocument():

    def __init__(self, id):
        self.id = id
        self.desktop = XSCRIPTCONTEXT.getDesktop()
        template_url = convert_path_to_url(join(TEMPLATE_PATH, TEMPLATE_NAME))
        args = (PropertyValue('Hidden', 0, False, 0),)
        self.model = self.desktop.loadComponentFromURL(template_url, "_default", 0, args)
        self.filename = FILENAME.format(id)
        self.save_path = join(SAVE_PATH, self.filename)

    def save(self):
        url = convert_path_to_url(self.save_path)
        args = (PropertyValue('FilterName', 0, 'MS Word 97', 0),)
        self.model.storeAsUrl(url, args)
        self.model.dispose()

g_exportedScripts = python_bal,

This is a very simple script I placed in ~/.config/libreoffice/4/user/Scripts/python/. I launch it from a button in the toolbar of an empty doc. I can't get through this error:

com.sun.star.uno.RuntimeExceptionError during invoking function python_bal in module file:///home/bastien/.config/libreoffice/4/user/Scripts/python/bbal.py (: storeAsUrl...)

I wonder if it doesn't come from the way I create self.desktop.


Solution

  • There is a typo in the method name : storeAsURL with URL solved the trick.

    Developing with uno component and python is full of pitfall because there is no simple IDE to help you autocompleting or inspecting code while developping. Yes, there is XrayTools...