Change all images anchor (without styles ?)


I have .odt documents with all figures anchored as character and aligned to the left.
I would like to have all these figures anchored to the paragraph and centered (the default). And of course I don’t want to do this one by one.
The figures style is unchanged (default anchor to the paragraph and centered) but the value has been changed for each figure and overwrite the style default. (NB : The documents have been created with pandoc from html files).
A command line solution would be ideal…

Thanks oweng (did nont find how to comment your answer). Exactly what I needed. I tried to do something like that before but I get corrupted odt file when I unziped/ziped it with right clic. It works however from the command line. The syntax of my odt file is quite different and do not match with your regular expression. But it seems that this is more a pandoc issue.

You don’t mention which platform you are using, but given your “command line” reference I will assume GNU/Linux. What you require is not simple to do if you only want some images altered in the manner described. If you want all images altered in this manner, then a basic hack of the XML is possible. You will need to write a script to:

  • Extract (unzip) content.xml from the ODT.
  • Change the text:anchor-type attribute of each draw:frame element with a draw:name="graphicsN" attribute in content.xml.
  • Replace (rezip) content.xml back in the ODT.

Note that images anchored “As character” as automatically aligned left, while images anchored “To paragraph” are automatically centre aligned. Bash script would look something like:

# change anchoring of all <draw:frame> elements from "as-char" to "paragraph"
# args: 1 = full ODT file name
rm -f $f_copy;
cp $1 $f_copy;
unzip $f_copy content.xml;
sed -i 's/draw:frame\(.*\)draw:name=\"graphics\(.*\)as-char/draw:frame\1draw:name=\"graphics\2paragraph/g' content.xml;
zip -fq $f_copy content.xml;

Each \(.*\) expression is a capturing group, which is substituted back in to the replacement string with \1 and \2. This is a crude replacement method that does not guarantee all settings will be as required in the resulting document. The document is copied however so it won’t ruin your original.