eea / odfpy

API for OpenDocument in Python
GNU General Public License v2.0
308 stars 63 forks source link

Add image to odt #45

Open beatorizu opened 7 years ago

beatorizu commented 7 years ago

Hi I'm trying to add picture to odt file, but is not working. How I could add image.

from odf.draw import Frame, Image, Page
from odf.style import PageLayout, MasterPage, Header, Footer
from odf.text import P
from odf import table
from datetime import datetime

template = load('/home/bea/Projects/Test Footer/templates/styled-template.odt')

pl = PageLayout(name="pagelayout")
template.automaticstyles.addElement(pl)
mp = MasterPage(name="Standard", pagelayoutname=pl)
page = Page(masterpagename=mp)
template.masterstyles.addElement(mp)

photoframe = Frame(width="200pt", height="200pt", x="56pt", y="56pt")
href = template.addPicture('/home/bea/Projects/Test Footer/code-icon-23069.png')
photoframe.addElement(Image(href=href))
page.addElement(photoframe)

template.save("pic.docx")
sammarrques commented 4 years ago

I have the same problem.

zimakim commented 4 years ago
from odf.opendocument import OpenDocumentText, OpenDocumentDrawing
from odf.draw import Frame, Image, Page
from odf.style import PageLayout, MasterPage, Header, Footer
from odf.text import P
from odf import table
from datetime import datetime

textdoc = OpenDocumentText()
p = P()
textdoc.text.addElement(p)
photoframe = Frame(width="200pt", height="200pt", x="56pt", y="56pt", anchortype="paragraph")
href = textdoc.addPicture('test.png')
photoframe.addElement(Image(href=href))
p.addElement(photoframe)

textdoc.save("pic.odt")
zimakim commented 4 years ago

With anchortype = "page"

from odf.opendocument import OpenDocumentText
from odf.draw import Frame, Image
from odf.style import Style, GraphicProperties

textdoc = OpenDocumentText()
frstyle = Style(name = 'frstyle', parentstylename="Graphics", family="graphic")
frstyle.addElement(GraphicProperties(verticalpos="from-top", verticalrel="page", horizontalpos="from-left", horizontalrel="page"))
textdoc.automaticstyles.addElement(frstyle)
photoframe = Frame(width="200pt", height="200pt", x="8cm", y="10cm", anchortype="page", stylename = frstyle)
href = textdoc.addPicture('test.png')
photoframe.addElement(Image(href=href))
textdoc.text.addElement(photoframe)
textdoc.save("pic.odt")