I finally have a solution - maybe others can benefit from it too. To run my code on a plain Ubuntu installation I recommend running
sudo apt-get install pip3
sudo pip install ooo-dev-tools
Once done, this python script can load the document, set any known user defined property, then save the document in any supported target format:
#!/bin/python3
import sys
from ooodev.utils.lo import Lo
from ooodev.utils.info import Info
from com.sun.star.beans import XPropertySet
from com.sun.star.beans import XPropertyAccess
doc = None
def main() -> int:
print (sys.argv)
with Lo.Loader(Lo.ConnectSocket(headless=True)) as loader:
i = 0
while i < len(sys.argv):
arg = sys.argv[i]
match arg:
case "--load":
i=i+1
docname = sys.argv[i]
print("loading " + docname)
doc = Lo.open_doc(fnm=docname, loader=loader)
case "--set":
i=i+1
val = sys.argv[i]
items = val.split("=")
name = items[0]
value = items[1]
print("setting " + name + "=>" + value)
user_props = Info.get_user_defined_props(doc) # XPropertyContainer
ps = Lo.qi(XPropertySet, user_props, True)
try:
ps.setPropertyValue(name, value)
except Exception:
pa = Lo.qi(XPropertyAccess, user_props, True)
names = []
for propertyvalue in pa.getPropertyValues():
names.append(propertyvalue.Name)
print ("Cannot set property '" + name + "'. Known properties are ", names)
return 1
case "--save":
i=i+1
docname = sys.argv[i]
print("saving " + docname)
Lo.save_doc(doc=doc, fnm=docname)
case _:
print (i, sys.argv[i])
i=i+1
return 0
if __name__ == "__main__":
raise SystemExit(main())
Once installed, the script can be invoked like
convert.py --load mydoc.odt --set propname=propvalue -save mydoc.pdf
Be aware you can use all the options multiple times and they are evaluated from left to right.
That means you can load a document, set multiple properties, save it into two output documents, load the next…