【问题标题】:inserting new line with OpenOffice::OODoc使用 OpenOffice::OODoc 插入新行
【发布时间】:2014-05-21 18:24:02
【问题描述】:

我在用这个模块创建一个新行时遇到了很大的问题,我觉得我只是错过了一些东西。

我的 perl 代码如下所示:

use OpenOffice::OODoc;

my $name = "foo <br> bar";
   $name=~s/<br>/\n/g;

my    $outdir = "template.odt";

    my $doc = ooDocument(file => $outdir);

    my @pars = $doc->getParagraphList();
    for my $p (@pars)
    {
        $doc->substituteText($p,'{TODAY}',$date);
        $doc->substituteText($p,'{NAME}',$name);
        ...

问题是当我在 word 或 open office 中打开它时,我没有换行符。虽然如果它在文本编辑中打开它我有我的新行..关于如何解决这个问题的任何想法?

【问题讨论】:

  • 您可能需要 LF + CR 换行。 $name =~ s/&lt;br&gt;/\r\n/g;
  • @Cfreak 刚试过,没有运气。

标签: perl


【解决方案1】:

好的,我想通了,希望这可以节省人们搜索相同内容的时间。我补充说:

use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
$doc->substituteText($p,'<br>', $linebreak);

所以我的最终代码如下所示:

use OpenOffice::OODoc;
use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
my    $outdir = "template.odt";

my $name = "foo <br> bar";

my    $outdir = "template.odt";

    my $doc = ooDocument(file => $outdir);

    my @pars = $doc->getParagraphList();
    for my $p (@pars)
    {
        $doc->substituteText($p,'{TODAY}',$date);
        $doc->substituteText($p,'{NAME}',$name);
        $doc->substituteText($p,'<br>', $linebreak);
        ...

也许不是最好的做事方式,但它确实有效!

【讨论】:

    【解决方案2】:

    您可以尝试在当前段落之后插入并清空段落:

    如果'text'选项为空,调用这个方法是等价的 添加换行符。

    此序列(在文本文档中)在第 4 段之后立即插入换行符。将 4 替换为当前位置。

            $doc->insertElement
                (
                '//text:p', 4, 'text:p',
                position        => 'after',
                text            => '',
                );
    

    【讨论】:

    • 刚试过这个并使用 $p 作为位置,似乎没有任何改变
    猜你喜欢
    • 2011-10-04
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多