[Solved] Python storeAsUrl raises AttributeError

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

[Solved] Python storeAsUrl raises AttributeError

Post by Bastien »

I try to create a document from a template, fill some textMasterFields and store it to a url. I'm stuck on the ultimate part: store my document.
Here is my code:

Code: Select all

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

def bal(*args):
    doc = BalDocument()

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

class BalDocument():
    def __init__(self):
        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.save_path = join(SAVE_PATH, self.filename)

    def save(self):
       url = convert_path_to_url(self.save_path)
        properties = []
        p = PropertyValue(
            Name = 'FilterName',
            Value = 'MS Word 97'
        )
        properties.append(p)
        self.model.storeAsUrl(url, properties)

The method save() fails. Url is correct. I suppose it may be because of properties. I've tried this too which didn't help:

Code: Select all

        properties = (
            PropertyValue("FilterName", 0, "MS Word 97", 0),
            PropertyValue("Overwrite", 0, True, 0),
                )
The output is:
com.sun.star.uno.RuntimeExceptionpyuno._createUnoStructHelper: member 'Handle' of struct type 'com.sun.star.beans.PropertyValue' not given a value. (Error during invoking function bal in module file:///home/bastien/.config/libreoffice/4/user/Scripts/python/rdt_bal.py (<class 'uno.com.sun.star.uno.RuntimeException'>: pyuno._createUnoStructHelper: member 'Handle' of struct type 'com.sun.star.beans.PropertyValue' not given a value.
Last edited by Bastien on Thu Oct 01, 2015 9:59 am, edited 2 times in total.
LibreOffice 6.2.3.2 on Ubuntu 19.04
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Python storeAsUrl raises AttributeError

Post by FJCC »

Does it work if you do

Code: Select all

p = PropertyValue()
p.Name = 'FilterName',
p.Value = 'MS Word 97'
self.model.storeAsURL(url, (p,) ) #Note the upper case URL
        
I believe the second argument passed to storeAsURL has to be a tuple.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: Python storeAsUrl raises AttributeError

Post by Bastien »

It was just a typo in method name. `URL` must be capitalized unlikely to this function `systemPathToFileUrl`. This kind of confusion creates a lot of pitfalls.

Do you know a way for having autocompletion in Python dev with uno and save lifetime?
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: Python storeAsUrl raises AttributeError

Post by karolus »

Bastien wrote:Do you know a way for having autocompletion in Python dev with uno and save lifetime?
For me it works from

Code: Select all

$ ipython notebook
..sessions

http://ipython.org/documentation.html

in the "notebook" to start the (soffice)-server:

Code: Select all

from subprocess import Popen

officepath = 'soffice'
calc = '--calc'
pipe = "--accept=pipe,name=abraxas;urp;StarOffice.Servicemanager"

Popen([officepath,
       calc,
       pipe]);
for access as "client":

Code: Select all

import uno
from pythonscript import ScriptContext

local = uno.getComponentContext()
resolver = local.ServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver")

client = resolver.resolve("uno:pipe,"
                           "name=abraxas;"
                           "urp;"
                           "StarOffice.ComponentContext")

createUnoService = client.ServiceManager.createInstance


XSCRIPTCONTEXT = ScriptContext(client, None, None)
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Solved] Python storeAsUrl raises AttributeError

Post by Bastien »

Amazing ! Thank you so much!
LibreOffice 6.2.3.2 on Ubuntu 19.04
Post Reply