2011-07-12 43 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',但沒有看到原始表中的樣式信息。我認爲這是我試圖解決的問題的關鍵。 –