使用 OpenOffice::OODoc 传输表格样式

发布于 2024-11-19 12:14:21 字数 6247 浏览 4 评论 0原文

我正在尝试将表格的格式从一个 OpenOffice Writer 文件复制到另一个文件...我可以看出我正在将样式名称写入第二个文档,但不是样式数据。

我怀疑这与 odfContainer 的 'styles' 部分有关,但我不清楚如何将其写入第二个文档,特别是因为当我在调试器中检查 $style 对象时,它似乎与 $doc 对象相同,据称该对象已加载 'content' > 部分。

这是我到目前为止所得到的...

#! /usr/bin/perl

use warnings;
use strict;

use OpenOffice::OODoc;

my $file='mytest.odt';
my $outfile='doc2.odt';

# load input file
my $container = odfContainer("$file");
$container->raw_export("styles.xml");
my $doc = odfDocument
        (
        container => $container,
        part      => 'content'
        );

my $style = odfDocument
        (
        container => $container,
        part      => 'styles'
        );

# load output file
my $container2 = odfContainer( $outfile, create => 'text' );
$container2->raw_import("styles.xml");

my $doc2 = odfDocument
        (
        container => $container2,
        part      => 'content'
        );


# Load table from 'mytest.odt'
my $table=$doc->getTable(0);

# Get style from first cell in $table
my $headerstyle=$doc->getStyle( $doc->getCell($table, 0, 0) );

# Create table in $doc2
my $newtable=$doc2->appendTable('newtable', 1, 1, 'table-style' => $doc->getStyle($table) );

# Set style of first cell in $newtable to 'Table1.A1'
$doc2->cellStyle( $newtable, 0, 0, 'Table1.A1' );

# Write 'doc2.odt' to disk
$container2->save;

我加载 'Table1.A1' 作为单元格样式的原因是我在 $table 深处发现了以下内容>,在调试器内部检查时:

'next_sibling' => OpenOffice::OODoc::Element=HASH(0x102029250)
   'att' => HASH(0x102029180)      
      'style:family' => 'table-cell'  
      'style:name' => 'Table1.A1'     
   'empty' => 0                    
   'first_child' => OpenOffice::OODoc::Element=HASH(0x1020294a0)
      'att' => HASH(0x102029200)      
         'fo:background-color' => '#cccccc'
         'fo:border' => '0.0069in solid #000000'
         'fo:padding-bottom' => '0in'    
         'fo:padding-left' => '0.075in'  
         'fo:padding-right' => '0.075in' 
         'fo:padding-top' => '0in'       
         'style:vertical-align' => 'top' 
         'style:writing-mode' => 'lr-tb' 

我知道属性与我尝试复制的内容相匹配,并且我还从实验中知道 'getStyle' 方法返回 style::name 属性...我只是不知道如何获取从使用 cellStyle 方法设置 style::name 属性到实际将基础数据写入新文档。

编辑:

解压缩 OpenOffice 文件,我得到几个 xml 文件:

  • settings.xml
  • styles.xml
  • content.xml

等。

'styles''content' 部分 < code>OdfContainer 对应styles.xml和content.xml。 Styles.xml 有点像 css 文件,包含 ODF 文件各个标头级别的样式信息。 Content.xml 还包含样式信息,很像 html 文档中的 css 标头。

这是从 odt 文件中提取的 content.xml 的样式部分(实际上很像......我没有保存原始文件)。

<?xml version="1.0" encoding="utf-8"?>
<office:document-content>
   ...
   <office:automatic-styles>
    <style:style style:name="Table6" style:family="table" style:master-page-name="First_20_Page">
      <style:table-properties style:width="6.9208in" style:page-number="auto" table:align="left" style:writing-mode="lr-tb" />
    </style:style>
    <style:style style:name="Table6.A" style:family="table-column">
      <style:table-column-properties style:column-width="1.2729in" />
    </style:style>
    <style:style style:name="Table6.B" style:family="table-column">
      <style:table-column-properties style:column-width="3.2604in" />
    </style:style>
    <style:style style:name="Table6.C" style:family="table-column">
      <style:table-column-properties style:column-width="2.3875in" />
    </style:style>
    <style:style style:name="Table6.1" style:family="table-row">
      <style:table-row-properties style:min-row-height="0.1597in" style:keep-together="true" fo:keep-together="auto" />
    </style:style>
    <style:style style:name="Table6.A1" style:family="table-cell">
      <style:table-cell-properties 
         style:vertical-align="bottom" 
         fo:background-color="#cccccc" 
         fo:padding-left="0.075in" 
         fo:padding-right="0.075in" 
         fo:padding-top="0in" 
         fo:padding-bottom="0in" 
         fo:border-left="0.0069in solid #000000" 
         fo:border-right="none" 
         fo:border-top="0.0069in solid #000000" 
         fo:border-bottom="0.0069in solid #000000" 
         style:writing-mode="lr-tb">
        <style:background-image />
      </style:table-cell-properties>
    </style:style>
 ...
  • style:name="Table6" 描述当前表格的样式,
  • style:name="Table6.A" 描述该表格 A 列的样式,
  • style:name="Table6.A1" 描述单元格 A1 的样式

对输入文件的“content.xml”部分进行原始导出,然后在输出文件中进行原始导入确实将数据从一个文件传输到另一个文件。

#! /usr/local/bin/perl

use warnings;
use strict;

use OpenOffice::OODoc;

my $infile=$ARGV[0];
my $outfile='outfile.odt';

my $incontainer = odfContainer( $infile );
$incontainer->raw_export("content.xml");

my $outcontainer = odfContainer( $outfile, create => 'text' );
$outcontainer->raw_import("content.xml");

$outcontainer->save;

运行 oodoc.pl infile.odt,然后解压缩 outfile.odt 并检查 content.xml 确实显示样式已成功转移:

<style:style style:name="Table1" style:family="table">
  <style:table-properties style:width="6.925in" table:align="margins" />
</style:style>
<style:style style:name="Table1.A" style:family="table-column">
  <style:table-column-properties 
   style:column-width="2.3083in" 
   style:rel-column-width="21845*" />
</style:style>
<style:style style:name="Table1.A1" style:family="table-cell">
  <style:table-cell-properties 
      fo:background-color="#cccccc" 
      fo:padding="0.0382in" 
      fo:border-left="0.0007in solid #000000" 
      fo:border-right="none" 
      fo:border-top="0.0007in solid #000000" 
      fo:border-bottom="0.0007in solid #000000">
    <style:background-image />
  </style:table-cell-properties>
</style:style>

既然已经完成了,我需要实际加载和使用 $outcontainer 中的单元格样式。

I'm attempting to copy the formatting of a table from one OpenOffice Writer file to another... I can tell that I'm writing the name of the style to the second document, but not the style data.

I suspect that this has something to do with the 'styles' part of the odfContainer, but I'm not clear on how to write this to the second document, especially because when I inspect the $style object in the debugger, it appears to be identical to the $doc object, which has supposedly loaded the 'content' part.

Here's what I've got so far...

#! /usr/bin/perl

use warnings;
use strict;

use OpenOffice::OODoc;

my $file='mytest.odt';
my $outfile='doc2.odt';

# load input file
my $container = odfContainer("$file");
$container->raw_export("styles.xml");
my $doc = odfDocument
        (
        container => $container,
        part      => 'content'
        );

my $style = odfDocument
        (
        container => $container,
        part      => 'styles'
        );

# load output file
my $container2 = odfContainer( $outfile, create => 'text' );
$container2->raw_import("styles.xml");

my $doc2 = odfDocument
        (
        container => $container2,
        part      => 'content'
        );


# Load table from 'mytest.odt'
my $table=$doc->getTable(0);

# Get style from first cell in $table
my $headerstyle=$doc->getStyle( $doc->getCell($table, 0, 0) );

# Create table in $doc2
my $newtable=$doc2->appendTable('newtable', 1, 1, 'table-style' => $doc->getStyle($table) );

# Set style of first cell in $newtable to 'Table1.A1'
$doc2->cellStyle( $newtable, 0, 0, 'Table1.A1' );

# Write 'doc2.odt' to disk
$container2->save;

The reason that I'm loading 'Table1.A1' as the cell style is that I found the following deep inside $table, when inspecting inside the debugger:

'next_sibling' => OpenOffice::OODoc::Element=HASH(0x102029250)
   'att' => HASH(0x102029180)      
      'style:family' => 'table-cell'  
      'style:name' => 'Table1.A1'     
   'empty' => 0                    
   'first_child' => OpenOffice::OODoc::Element=HASH(0x1020294a0)
      'att' => HASH(0x102029200)      
         'fo:background-color' => '#cccccc'
         'fo:border' => '0.0069in solid #000000'
         'fo:padding-bottom' => '0in'    
         'fo:padding-left' => '0.075in'  
         'fo:padding-right' => '0.075in' 
         'fo:padding-top' => '0in'       
         'style:vertical-align' => 'top' 
         'style:writing-mode' => 'lr-tb' 

I know that the attributes match what I'm trying to copy, and I also know from experimentation that the 'getStyle' method returns the style::name attribute... I just don't know how to get from setting the style::name attribute using the cellStyle method to actually having the underlying data written in to the new document.

Edit:

Unzipping the OpenOffice file, I get several xml files:

  • settings.xml
  • styles.xml
  • content.xml

etc.

The 'styles' and 'content' parts of the OdfContainer correspond to styles.xml and content.xml. Styles.xml is a little like a css file, containing the style information for various header levels of an ODF file. Content.xml also contains style information, much like the css header in an html document.

Here's the style part of content.xml extracted from the odt file (actually one much like it... I didn't save the original).

<?xml version="1.0" encoding="utf-8"?>
<office:document-content>
   ...
   <office:automatic-styles>
    <style:style style:name="Table6" style:family="table" style:master-page-name="First_20_Page">
      <style:table-properties style:width="6.9208in" style:page-number="auto" table:align="left" style:writing-mode="lr-tb" />
    </style:style>
    <style:style style:name="Table6.A" style:family="table-column">
      <style:table-column-properties style:column-width="1.2729in" />
    </style:style>
    <style:style style:name="Table6.B" style:family="table-column">
      <style:table-column-properties style:column-width="3.2604in" />
    </style:style>
    <style:style style:name="Table6.C" style:family="table-column">
      <style:table-column-properties style:column-width="2.3875in" />
    </style:style>
    <style:style style:name="Table6.1" style:family="table-row">
      <style:table-row-properties style:min-row-height="0.1597in" style:keep-together="true" fo:keep-together="auto" />
    </style:style>
    <style:style style:name="Table6.A1" style:family="table-cell">
      <style:table-cell-properties 
         style:vertical-align="bottom" 
         fo:background-color="#cccccc" 
         fo:padding-left="0.075in" 
         fo:padding-right="0.075in" 
         fo:padding-top="0in" 
         fo:padding-bottom="0in" 
         fo:border-left="0.0069in solid #000000" 
         fo:border-right="none" 
         fo:border-top="0.0069in solid #000000" 
         fo:border-bottom="0.0069in solid #000000" 
         style:writing-mode="lr-tb">
        <style:background-image />
      </style:table-cell-properties>
    </style:style>
 ...
  • style:name="Table6" describes the style for the current table,
  • style:name="Table6.A" describes the style for column A of this table,
  • style:name="Table6.A1" describes the style for the cell A1

Doing a raw export of 'content.xml' section of the input file, then a raw import in the output file does transfer data from one file to the other.

#! /usr/local/bin/perl

use warnings;
use strict;

use OpenOffice::OODoc;

my $infile=$ARGV[0];
my $outfile='outfile.odt';

my $incontainer = odfContainer( $infile );
$incontainer->raw_export("content.xml");

my $outcontainer = odfContainer( $outfile, create => 'text' );
$outcontainer->raw_import("content.xml");

$outcontainer->save;

Running oodoc.pl infile.odt, Then unzipping outfile.odt and inspecting content.xml does show that the style has been successfully transferred:

<style:style style:name="Table1" style:family="table">
  <style:table-properties style:width="6.925in" table:align="margins" />
</style:style>
<style:style style:name="Table1.A" style:family="table-column">
  <style:table-column-properties 
   style:column-width="2.3083in" 
   style:rel-column-width="21845*" />
</style:style>
<style:style style:name="Table1.A1" style:family="table-cell">
  <style:table-cell-properties 
      fo:background-color="#cccccc" 
      fo:padding="0.0382in" 
      fo:border-left="0.0007in solid #000000" 
      fo:border-right="none" 
      fo:border-top="0.0007in solid #000000" 
      fo:border-bottom="0.0007in solid #000000">
    <style:background-image />
  </style:table-cell-properties>
</style:style>

Now that this has been done, I'll need to actually load and use the cell styles in $outcontainer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-11-26 12:14:21

您进行了原始导入。该文档表示“还要记住,在保存之前,导入实际上并不是由 OODoc::File 执行的,因此导入的数据不会立即可用。”我建议您尝试 $container2->save; ,然后在导入样式后立即重新加载它,然后在下次保存后查看 Table.A1 是否显示在 doc2.odt 的 content.xml 中:

# load output file

my $container2 = odfContainer( $outfile, create => 'text' );
$container2->raw_import("styles.xml");

# Carry out the import and reload it with the new styles.
$container2->save;

$container2 = odfContainer( $outfile );

my $doc2 = odfDocument
        (
        container => $container2,
        part      => 'content'
        );


# Load table from 'mytest.odt'
my $table=$doc->getTable(0);

# Get style from first cell in $table
my $headerstyle=$doc->getStyle( $doc->getCell($table, 0, 0) );

# Create table in $doc2
my $newtable=$doc2->appendTable('newtable', 1, 1, 'table-style' => $doc->getStyle($table) );

# Set style of first cell in $newtable to 'Table1.A1'
$doc2->cellStyle( $newtable, 0, 0, 'Table1.A1' );

# Write 'doc2.odt' to disk
$container2->save;

You did a raw import. The docs for that say "Remember too that the import is not actually carried out by OODoc::File until a save and the imported data is therefore not immediately available." I suggest you try $container2->save; and then re-load it right after importing styles and then see if Table.A1 shows up in doc2.odt's content.xml after the next save:

# load output file

my $container2 = odfContainer( $outfile, create => 'text' );
$container2->raw_import("styles.xml");

# Carry out the import and reload it with the new styles.
$container2->save;

$container2 = odfContainer( $outfile );

my $doc2 = odfDocument
        (
        container => $container2,
        part      => 'content'
        );


# Load table from 'mytest.odt'
my $table=$doc->getTable(0);

# Get style from first cell in $table
my $headerstyle=$doc->getStyle( $doc->getCell($table, 0, 0) );

# Create table in $doc2
my $newtable=$doc2->appendTable('newtable', 1, 1, 'table-style' => $doc->getStyle($table) );

# Set style of first cell in $newtable to 'Table1.A1'
$doc2->cellStyle( $newtable, 0, 0, 'Table1.A1' );

# Write 'doc2.odt' to disk
$container2->save;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文