Info: How to make batch changes to odt files to change things like margins, etc in Linux

I figured out a way to make batch changes to odt files in Linux to do things like change the margins. This will also work for other changes like font sizes, font names and many other features (such as adding a savedate to the footer of the document.

WARNING: Don’t work on your original files. Copy them to a new folder, make the change, then check your files to see if it worked.

This works best when you have consistent files i.e. all my files have margins set and I want to change them to a different size.

Below is a simple example of a way to change all margins from the default 0.79" to 0.5".

Steps:
Create a copy of the files in html. The following command creates copies of all odt files, keeping the same name but saving it in html format which is readable.
$libreoffice --convert-to html *.odt

Now, I open one of the html files using a text editor like gedit and I see a lone beginning with <style> and having margin: 0.79in. This is what I want to change to be 0.5 inches. If I manually make the change at this point in gedit, then save the file as test.html, I can then convert test.html to test.odt with $libreoffice --convert-to odt test.odt and run writer with this odt file to verify my change worked.

If it worked, I now want to do the same for all my files.

Delete the test.html and test.odt so I don’t get confused. Then issue the following command:

$for i in $(ls *.html)
>do
>sed --in-place 's/margin: 0.79in/margin: 0.50in/' $i
>done

The first line starts a loop. The *ls .html finds all my html files and then passes them one by one to the variable i.
The second line starts what I want to do for each value in i
The third line runs the string editor which substitutes the margin setting I found in the html file and replaces it with my new margin setting and it is done in-place i.e. put back into the original file
The last line completes the loop and will force return back to the beginning until we run out of file names.

We now have changed all the files that have the old margin setting and replaced them with the new one. So it’s time to convert back to odt format.
$libreoffice --convert-to odt *.html

We are finished.

Note that the sed command is a powerful editor and can use all sorts of conditions and wildcards to convert not only fixed strings of information but variable ones, inserting fields in footnotes, and making changes only if certain conditions are met.

This may work but it will botch all clever formatting present in the original file.

I suspect you practice direct formatting, i.e. everything is done manually, individually on all instances of text.

With rigorous styling formatting, you can do the same simply by modifying one or two style definitions (page styles for margins) and you don’t lose your style markup because you avoid the HTML → ODF conversion on return. Remember that Writer is not an HTML editor (though it can output HTML); CSS has no one-to-one mapping to styles and that CSS cascading is not the same as style inheritance.

My best recommendation is: learn to work with styles.

If you can suggest a better way of mass changing files, I’d love to see an example. I’ve searched the net and have not found any other way to do it.

If the non-profit I have been helping had first set up a template, they would have saved themselves a lot of trouble. Now they have a couple of hundred Information sheets they would like to modify to be consistent with how they want them all to look. I couldn’t find a way to do it within libreoffice so I came up with this method. The only batch changing of files seems to be the ability to go from one file type to another e.g. docx to pdf or odt to doc.

It’s easy to say “learn this” than it is to show an example of how it’s done or to post a link to such a thing. I would be greatly pleased to see a sticky or other posting showing an example of how to do mass editing of files using styles.

Two variants of your method:

  • You could save as or convert to .fodt Then give sed the xml-file
  • Realize a .odt-file is a zip-archive, where you can extract contents and re-pack after editing the xml.

This is a typical case where initial preparation is very important. If all documents must have the same look, all collaborating writers must adhere to a very strict discipline: formatting is done by applying agreed styles. And to make things easy, all styles (and perhaps initial contents like header and footer, aka. similar to stationery) are recorded in a template file (extension .ott – this is not what laymen ordinarily call a “template”). Then modifying the template styles magically updates the documents next time they are opened.

Explaining the advantages of styles and their workflow is too long for a comment. See Bruce Byfield’s Designing with LO for a clear presentation. Document is downloadable after pressing “More…” and scrolling down a bit. You can also download the Writer Guide from the same link and read the chapters dedicated to styles but this is less of a tutorial.

1 Like

like where ? :thinking:

  1. how to write macro to set page margins and orientation
  2. [Writer/Macro] getCurrentSelection() returns null when macro is invoked headless
    https://superuser.com/questions/1135850/how-do-i-run-a-libreoffice-macro-from-the-command-line-without-the-gui

full API : LibreOffice: Class List
higher level (“dispatch”) : https://wiki.documentfoundation.org/Development/DispatchCommands