2011-07-12 47 views
34

我在尝试将表格的格式从一个OpenOffice Writer文件复制到另一个...我可以告诉我正在将样式的名称写入第二个文档,但不是样式数据。使用OpenOffice传输表格样式:: OODoc

我怀疑这事做与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属性知道......我只是不知道如何从设定得style::name属性使用cellStyle方法实际将基础数据写入新文档。

编辑:

解压了OpenOffice文件,我得到几个XML文件:

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

等。

Th e 'styles''content'部分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> 
... 
  • 风格:NAME = “表6” 说明样式当前表,
  • 风格:NAME = “Table6.A” 描述了这个表的A列中的风格,
  • 风格:名称=“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并检查内容。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使用单元格样式。

+2

我不是一个Perl的用户,但我已经在PHP和C++中完成了。它实际上是这样的:取出文档,打开样式和内容,用新数据替换数据文档的单元格值,将整个软件包打包成一个新的软件包...完成。如果你想改变样式,它只会变得更复杂。我个人将XML归咎于怪异的重新列表,但我也知道XML是逻辑的,并不简单。无论如何...希望我给你一个提示。你需要自己编写代码。 – 2012-08-01 17:30:38

回答

1

你做了一个原始的导入。该文档说:“还要记住,导入并不是由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; 
+0

嗯。这是一个非常有趣的方法。样式信息实际上在content.xml中(命名样式和默认样式保存在styles.xml中,'自动'样式属于content.xml中)。话虽如此,我会试一试,看看会发生什么...... –

+0

我在调试器中检查了'$ newtable',但没有看到原始表中的样式信息。我认为这是我试图解决的问题的关键。 –