Perl 使用OpenOffice::OODoc传输表格样式
我正在尝试将表的格式从一个OpenOffice Writer文件复制到另一个。。。我可以看出我正在将样式的名称写入第二个文档,但没有写入样式数据 我怀疑这与Perl 使用OpenOffice::OODoc传输表格样式,perl,openoffice.org,openoffice-writer,Perl,Openoffice.org,Openoffice Writer,我正在尝试将表的格式从一个OpenOffice Writer文件复制到另一个。。。我可以看出我正在将样式的名称写入第二个文档,但没有写入样式数据 我怀疑这与odfContainer的'style'部分有关,但我不清楚如何将其写入第二个文档,特别是因为当我在调试器中检查$style对象时,它似乎与$doc对象相同,它应该已经加载了“内容”部分 这是我到目前为止得到的 #! /usr/bin/perl use warnings; use strict; use OpenOffice::OODoc
odfContainer
的'style'
部分有关,但我不清楚如何将其写入第二个文档,特别是因为当我在调试器中检查$style
对象时,它似乎与$doc
对象相同,它应该已经加载了“内容”
部分
这是我到目前为止得到的
#! /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
OdfContainer
的'styles'
和'content'
部分对应于styles.xml和content.xml。xml有点像css文件,包含ODF文件不同头级别的样式信息。xml还包含样式信息,很像html文档中的css标题
下面是从odt文件中提取的content.xml的样式部分(实际上很像它…我没有保存原始的)
运行oodoc.pl infle.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
中的单元格样式。文档中说“记住,在保存之前,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;
我不是Perl用户,但我在PHP和C++中做的很相似。它实际上是这样的:获取文档,打开样式和内容,用新数据替换数据文档的单元格值,将整个包打包成一个新包。。。完成。如果你也想改变样式的话,事情只会变得复杂一点。我个人将奇怪的重新排序问题归咎于XML,但我也知道XML是逻辑的,而不是简单的。无论如何希望我给了你一个暗示。你需要自己编写代码。这是一个非常有趣的方法。样式信息实际上在content.xml中(命名样式和默认样式保存在styles.xml中,“自动”样式属于content.xml)。话虽如此,我还是试一试,看看会发生什么……我在调试器中检查了$newtable
,但没有看到原始表中的样式信息。我认为这是我试图解决的问题的关键。
<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>
# 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;