Thread

Posted on Thu Sep 14 19:30:32 2006 by littleslinux
create a new empty odt file and change the style properties
I want to write a script file to create new odt file with modified style as :
$document = ooDocument ( file => 'test.odt', create => 'text' ) or print " can not create $filename! \n"; $document->save; $style = ooDocument ( file => 'test.odt', member => 'styles', ) or print " can not open document.odt styles ! \n"; $style->updateStyle ( 'Text body', properties => { 'area' => 'text', 'fo:font-size' => "8pt", 'style:font-name' => 'Times' } ) or print " can updateStyle $LINE!\n" ; $style->save("test.odt"); $content->appendParagraph ( text => 'Text body content', style => 'Text body' ); $content->save("test.odt");
error message as :
[OpenOffice::OODoc::Styles::updateStyle] Unknown style
It seems can't change 'Text body' properties in this way?????????? Thanks, LL
Direct Responses: 3032 | Write a response
Posted on Thu Sep 14 22:24:22 2006 by jeunice in response to 3030
Re: create a new empty odt file and change the style properties

So, two problems that I see:

(1) OpenOffice has both internal and external (display) style names. The main difference is that spaces are replaced with '_20_', so that 'Text body' becomes 'Text_20_body' in the XML representation. Given your request, the updateStyle code eventually asks for a style with attribute style:name="Text body"--and there is no such style. There is, however, a style with style:display-name="Text body" and style:name="Text_20_body" attributes. Solution: Ask for the internal style name (which you can easily arrive at with a simple substitution).

(2) I don't know where your variable $content is defined, but it isn't properly initialized in the code you provided.

Try this code, instead:

use OpenOffice::OODoc; $document = ooDocument ( file => 'test.odt', create => 'text' ) or print " can not create $filename! \n"; $document->save; $style = ooDocument ( file => 'test.odt', member => 'styles', ) or print " can not open document.odt styles ! \n"; $style->updateStyle ( 'Text_20_body', properties => { 'area' => 'text', 'fo:font-size' => "8pt", 'style:font-name' => 'Verdana' } ) or print " can't updateStyle $LINE!\n" ; $style->save("test.odt"); $document = ooDocument ( file => 'test.odt', ) or print " can not create $filename! \n"; $document->appendParagraph ( text => 'Text body content', style => 'Text_20_body' ); $document->save("test.odt");
Direct Responses: 3035 | Write a response
Posted on Fri Sep 15 00:17:06 2006 by jmgdoc in response to 3032
Re: create a new empty odt file and change the style properties

This second version should work, but it's sub-optimal, due to repeated and useless "saves".
It's neither required not useful to immediately save the newly created document, then re-open it in order to update the "Text body" (Text_20_body) style, then to re-save it, then re-open it to append a paragraph using the updated style. The very last save() only is useful (and it could be issued without argument, because the target file is the current one).

The $style object should be created as a Document object instance using the same file as the $document object. To do so, the "file" option of ooDocument() should provide, for $style, the reference of the $document object instead of the file name.

So, the same example could be rewritten like that:
$document = ooDocument(file => "test.odt", member => "content") or die "Can't create test.odt\n"; $style = ooDocument(file => $document, member => "styles") or die "Can't get access to the styles of test.odt (looks strange)\n"; $style->updateStyle ( "Text_20_body", properties => { area => "text", 'fo:font-size' => "8pt", 'style:font-name' => 'Verdana' } ) or print "Can't updateStyle $LINE!\n"; $document->appendParagraph ( text => 'Text body content', style => 'text_20_body' ); $document->save;

A similar example, and more explanations, are provided in this article, below the section "Content, Styles, and Metadata".
Write a response