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.
