| 1 |
#!/usr/bin/perl |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odfextract 0.4 2008-05-04 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odfextract - Text selection and copy from one document to another one |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
This sample script extracts every text element matching a given |
| 13 |
pattern (string or regex) from a source document and appends it |
| 14 |
to a target document. The target document must be an existing one |
| 15 |
(empty or not). |
| 16 |
|
| 17 |
The first argument of the command line is the source file name, |
| 18 |
the second one is the target file, the third one is the selection |
| 19 |
filter (i.e. any quoted string or Perl regular expression) |
| 20 |
|
| 21 |
=cut |
| 22 |
|
| 23 |
use OpenOffice::OODoc 2.101; |
| 24 |
|
| 25 |
die "Usage : odfextract source_file target_file filter\n" |
| 26 |
unless ($ARGV[0] && $ARGV[1] && $ARGV[2]); |
| 27 |
|
| 28 |
# document objects initialization |
| 29 |
print "Opening $ARGV[0] as source document...\n"; |
| 30 |
my $source_doc = odfText(file => $ARGV[0]) |
| 31 |
or die "$ARGV[0] is not a regular ODF file\n"; |
| 32 |
print "Opening $ARGV[1] as target document...\n"; |
| 33 |
my $target_doc = odfText(file => $ARGV[1]) |
| 34 |
or die "$ARGV[1] is not a regular ODF file\n"; |
| 35 |
|
| 36 |
# the imported elements will be appended to the document body |
| 37 |
my $root_element = $target_doc->getBody; |
| 38 |
|
| 39 |
# selection & transfer are done here |
| 40 |
print "Selecting the content matching $ARGV[2]...\n"; |
| 41 |
$target_doc->appendElement($root_element, $_) |
| 42 |
for $source_doc->selectElementsByContent($ARGV[2])->copy; |
| 43 |
|
| 44 |
# commit the changes in target file |
| 45 |
print "Saving the selection in $ARGV[1]...\n"; |
| 46 |
$target_doc->save; |
| 47 |
|
| 48 |
print "Job complete\n"; |
| 49 |
exit; |
| 50 |
|
| 51 |
#---------------------------------------------------------------------- |