DEV Community

Discussion on: Make LibreOffice Draw Document to use odfpy

Collapse
 
arachan profile image
Yusuke arakawa • Edited

I add Picture use LibreOffice Writer.
I read this documents's context.xml.

It's under code.

it add text.p in frame.
it add draw image in frame.

Add image to odt #45
He is mistake.
He should add frame in text.P .

Test code?
Not yet.

<?xml version="1.0" encoding="UTF-8"?>
...

Omission
...
<office:body>
<office:text>
<text:sequence-decls>
<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
<text:sequence-decl text:display-outline-level="0" text:name="Figure"/>
</text:sequence-decls>
<text:p text:style-name="Standard">
<draw:frame draw:style-name="fr1" draw:name="イメージ1" text:anchor-type="paragraph" svg:width="17cm" svg:height="5.736cm" draw:z-index="0">
<draw:image xlink:href="Pictures/1000020100000C8000000438BDD44B89CE152A7F.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" loext:mime-type="image/png"/>
</draw:frame>
</text:p>
</office:text>
</office:body>
</office:document-content>

Thread Thread
 
arachan profile image
Yusuke arakawa • Edited

Test Code.
Work Fine!
It is Point that add picture frame in paragraph element.

from odf.opendocument import OpenDocumentText
from odf.text import P,ParagraphProperties
from odf import teletype
from odf.style import Style
from odf.draw import Frame,Image

# Justified style
justifystyle = Style(name="justified", family="paragraph")
justifystyle.addElement(ParagraphProperties(attributes={"textalign": "justify"}))

# Creating different style used in the document
doc=OpenDocumentText()
s=doc.styles
s.addElement(justifystyle)

# Adding a paragraph
paragraph_element = P(stylename=justifystyle)
paragraph_text="hello world"
teletype.addTextToElement(paragraph_element, paragraph_text)
doc.text.addElement(paragraph_element, paragraph_text)

# add picture
picframe=Frame()
href=doc.addPicture("drawlogo.png")
picframe.addElement(Image(href=href))
# Point!
# add picture frame in paragraph element
paragraph_element.addElement(picframe)

# save ODT
doc.save("new.odt")


Thread Thread
 
arachan profile image
Yusuke arakawa

Test Code.
Add element in Frame.
Add element in Image.


from odf.opendocument import OpenDocumentText
from odf.text import P
from odf import teletype
from odf.style import Style,ParagraphProperties
from odf.draw import Frame,Image

# Justified style
justifystyle = Style(name="justified", family="paragraph")
justifystyle.addElement(ParagraphProperties(attributes={"textalign": "justify"}))

# Creating different style used in the document
doc=OpenDocumentText()
s=doc.styles
s.addElement(justifystyle)

# Adding a paragraph
paragraph_element = P(stylename=justifystyle)
paragraph_text="hello world"
teletype.addTextToElement(paragraph_element, paragraph_text)
doc.text.addElement(paragraph_element, paragraph_text)

# add picture
picframe=Frame(anchortype="paragraph",height='26mm',width='26mm',name='image1')
href=doc.addPicture("drawlogo.png")
picframe.addElement(Image(href=href,actuate="onLoad",show="embed",type="simple"))
paragraph_element.addElement(picframe)

# save ODT
doc.save("new.odt")