Silly script: Manipulate Ooo properties variables

[[Thanks to Reza too!]]

I like the

File > Properties > Custom properties ...

option in (and LibreOffice) where I can specify different parameters/variables in my documents. This could be revision numbers, repo indicators, and what have you.

Now, since the Ooo actually stores the documents in a zipped xml format, the variables can be accessed through the linux domain with for example:

KEY="Document number"
unzip -pa $oooFile meta.xml | tr -s "<" "\n" | grep "$KEY" | tr -s ">" "\n" | tail -n 1

(Yes, there is a flaw, since if there would be $KEY’s that are substrings of each other, it could fail, I’ll leave to the happy hacker to fix that and post here in that case. I’ll solve it by choosing my property names.).

The Ooo file is unzipped, but only the meta.xml is extracted. We then replace the “<" with carriage returns to split the XML tags from their contents. We also strip at the end of the value of the property.

Now, encapsulating this in a function, for example is easy as a cake. And multiple variables can be extracted on another level, for example:

KEY="Document repo"
docRepo=` $oooFile "$KEY"`
KEY="Document number"
docNo=` $oooFile "$KEY"`
KEY="Document type"
docType=` $oooFile "$KEY"`
KEY="Document tag"
docTag=` $oooFile "$KEY"`
KEY="Document revision"
docRev=` $oooFile "$KEY"`
echo "${docRepo}_${docNo}_${docType}_${docTag}_${docRev}"

Using the same approach, it is also easy to modify the existing property and replace the meta.xml file in the zipped archive. Through linux we can therefore create and change the variables. This is quite handy in a documention versioning system – or if you want to bulk change names or sanity check variables settings in large batch of files.


One thought on “Silly script: Manipulate Ooo properties variables

  1. Nice!

    For the manipulation part of variables, something like this could be used (might need some nice formatting to be readable…):


    #usage: tag value file.odt
    #replaces all occurances of xxx with value in file.odt
    unzip -pa $3 meta.xml | sed “s/\(\)\(.*\)\(\)/\1$2\3/g” > /tmp/meta.xml

    zip -u -j $3 /tmp/meta.xml

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s