使用lpOD进行注释检索

时间:2014-06-06 07:39:42

标签: python perl lpod

我在content.xml文件的test.odt中有此块:

<office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls><text:p text:style-name="Standard"/><text:p text:style-name="Standard">Hello <office:annotation office:name="__Fieldmark__4_822755319"><dc:creator>Unknown Author</dc:creator><dc:date>2014-06-06T09:29:21.051317276</dc:date><text:list text:style-name=""><text:list-item><text:p text:style-name="P1"><text:span text:style-name="T1">This is a comment</text:span></text:p></text:list-item></text:list></office:annotation>World<office:annotation-end office:name="__Fieldmark__4_822755319"/>, Hello Simple ODF!</text:p></office:text></office:body>

我正在尝试使用Perl ODF::lpOD库。例如,我能够提取主段落的内容:

use ODF::lpOD;
$doc = odf_get_document("test.odt")
                or die "Failed to load the document\n";
$contenu = $doc->get_body;
$p = $contenu->get_paragraph(position=>-2);
$texte = $p->get_text;
print $texte

output> Hello World, Hello Simple ODF!

但是我无法使用get_annotation函数来提取有关注释的信息。例如,我的一次尝试:

use ODF::lpOD;
$doc = odf_get_document("test.odt")
                or die "Failed to load the document\n";
$contenu = $doc->get_body;
$p = $contenu->get_paragraph(position=>-2);
print "\n";
$annot = $p->get_annotations; # output is the same with $annot=$contenu->get_annotations
print $annot

output> 1

这是1是什么?我想使用函数get_dateget_author等来获取有关注释的信息。

1 个答案:

答案 0 :(得分:0)

来自man ODF::lpOD::TextElement

"get_annotations()" always returns a list

你在标量上下文中使用了这个函数,所以你看到列表中的元素数量只是1而不是列表。如果我们运行这个示例程序并查看输出,你可以看看文档结构是什么:

#!/usr/bin/perl -w
# -Ian! D. Allen - idallen@idallen.ca - www.idallen.com

use strict;
use ODF::lpOD;

my $doc = odf_new_document('text')
    or die "$0: Failed to create new text\n   ";

my $content = $doc->get_part(CONTENT) or die;
my $context = $content->get_body or die;
$context->append_element(
    odf_paragraph->create(
           style => "Standard",
           text => "Hello World !"
    )
);
my $p = $context->get_paragraph(position=>1) or die;
my $t = $p->get_text or die;
print "$t\n";
$p->set_annotation(
    text => "This is an annotation.",
    after => "World",
) or die;

my @as = $p->get_annotations() or die;
for my $a ( @as ) {
    &Dump("", $a);
}

sub Dump {
    my $k = shift;
    my $v = shift || '';
    if ( ref($v) ne '' ) {
        for my $k2 ( keys %{$v} ) {
            my $v2 = $$v{$k2};
            if ( $k2 ne 'parent' && $k2 ne 'last_child' && $k2 ne 'prev_sibling' ) {
                my $r = &Pretty($v);
                print "$k $r\[$k2] => ";
                &Dump("$k $r", $v2);
            }
        }
    } else {
        print " $v\n";
    }
}

sub Pretty {
    my $r = shift;
    local $_ = ref($r);
    s/ODF::lpOD:://;
    return $_;
}

$doc->save(target => "helloworld.odt");
exit;

<强>输出:

Hello World !
 Annotation[next_sibling] =>  Annotation TextNode[next_sibling] =>  
 Annotation TextNode[gi] =>  1
 Annotation TextNode[pcdata] =>   !
 Annotation[gi] =>  29
 Annotation[style] =>  
 Annotation[first_child] =>  Annotation Element[next_sibling] =>  Annotation Element Element[next_sibling] =>  Annotation Element Element Paragraph[gi] =>  28
 Annotation Element Element Paragraph[first_child] =>  Annotation Element Element Paragraph TextNode[next_sibling] =>  
 Annotation Element Element Paragraph TextNode[gi] =>  1
 Annotation Element Element Paragraph TextNode[pcdata] =>  This is an annotation.
 Annotation Element Element Paragraph[next_sibling] =>  
 Annotation Element Element[gi] =>  15
 Annotation Element Element[first_child] =>  Annotation Element Element TextNode[pcdata] =>  2016-06-11T05:04:36
 Annotation Element Element TextNode[next_sibling] =>  
 Annotation Element Element TextNode[gi] =>  1
 Annotation Element[gi] =>  17
 Annotation Element[first_child] =>  Annotation Element TextNode[gi] =>  1
 Annotation Element TextNode[next_sibling] =>  
 Annotation Element TextNode[pcdata] =>  idallen