Problem: have a set of ODT files (tech transfer contracts) that I need to update, but not just now, because they contain a stale proprietary notice that needs to be changed. Perhaps I will never update them all, but only a few. So I prefer to put a notice in each of them to say “hey, you haven't updated notice X yet”. They are hundreds in tens of folders and subfolders.
Solution: Luckily they are ODF files, ODF is a zipped XML, a PERL module exists to do incredible tricks.
I have then made this basic script, just tweaking the example provided:
#/bin/perl! #filename to be passed as an argument use OpenOffice::OODoc; # get global access to the content of an ODF file my $document = odfDocument(file => $ARGV ); # select a text element containing a given string my $place = $document->getParagraph(0); # insert a new text element before the selected one my $newparagraph = $document->insertParagraph ( $place, position => 'before', text => 'A COOL new paragraph to be inserted at the beginning of the file', style => 'Text body' ); # save the modified document $document->save;
I know it is slightly less than a hack, but do that with a MS Word document.
Installing the module is pretty straightforward, it only requires an additional module to manage zip files, as dependancy, on my Ubuntu 10.10 standard PERL installation.
Many Thanks to Ingolf Schaefer (@firstname.lastname@example.org) for the hints. Also Giuseppe Maxia pointed in the right direction.