in reply to Re: Search OpenOffice document for title and author
in thread Search OpenOffice document for title and author [Solved]

The meta data doesn't show the title which is a paragraph with the style of 'Title' or Psomething.

I installed ODF::lpOD once I figured out that lpOD starts with lower case L. It's documentation is voluminous and not much better than OpenOffice::OODoc's.

However I did get it to work!

#!/usr/bin/perl -w use strict; use ODF::lpOD; my $file = $ARGV[0]; die "You must supply an odf file name\n" unless $file; my $doc = odf_document->get($file) or die "Can't load $file\n$!\n"; my $context = $doc->body; my $meta = $doc->meta; # Doesn't do what I wanted my $title = $meta->get_title; print "Title: $title\n" if $title; # shows Title: c if at all my $p = $context->get_paragraph(style=>'Title', content=>'ODF',positio +n=>0); if ($p) {print $p->get_text()."\n";} else {print "Not found\n";} # prints Not found #this works my ($i,$ps,$style,$pStyle); for ($i = 0; $i < 6; $i++) { $p = $context->get_paragraph(position=>$i); if ($p) { print "Paragraph $i"; $ps = $p->get_style(); if ($ps) { if ($ps =~ m/^P\d+$/) { # Check for internal styles $style = $doc->get_style('paragraph',$ps) || ''; $pStyle = $style->get_parent_style if $style; $ps = $pStyle if $pStyle; # This gets the real name } print " Style: $ps\n"; } else {print "No Style\n";} my $data = $p->get_text(recursive=>1); if ($data) {print "$data\n";} else {print "--No data\n";} } else { print "Paragraph $i not found\n"; } }

Thanks Ken

—Bob

Replies are listed 'Best First'.
Re^3: Search OpenOffice document for title and author
by jinnicky (Sexton) on Feb 26, 2016 at 02:33 UTC

    The ODF::LpOD modules worked on .odf files but choked on .sxw (version 1.1) files.

    Ken's suggestion about the similarity between those modules and the OpenOffice::OODoc modules helped me to go back to them and come up with this code:

    #!/usr/bin/perl use warnings; use strict; use OpenOffice::OODoc; # setup file and styles my $file = $ARGV[0]; die "You must supply an odf file name\n" unless $file; print "File: $file\n"; my $container = odfContainer($file) or die "Can't get document $file\n +"; my $doc = odfDocument(container => $container, part => 'content' +) or die "Can't get content in $file\n"; my ($i,$element,$text,$style); for ($i = 0; $i < 10; $i++) { $element = $doc->getElement('//text:p',$i); if ($element) { $text = $doc->getText($element); if ($text) { $style = $doc->getAttribute($element,'style name')||''; if ($style && ($style =~ m/^P\d+$/)) { $style = $doc->getAncestorStyle($style); } print "Paragraph $i: "; print "($style) " if $style; print "$text" if $text; print "\n"; } } }

    —Bob