DEV Community

Discussion on: Make LibreOffice Draw Document to use odfpy

 
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")