Set anchortype="paragraph" for image using odf.text

60 Views Asked by At

I want to change https://github.com/turulomio/pdf2odt so that images are 21cm wide (that was easy) and so that images are anchored to paragraph instead of as-char, so that the image goes all the way to the edge of the page.

The ODT file contains:

    <draw:frame draw:style-name="fr1" draw:name="Frame.3" text:anchor-type="as-char" svg:width="21.001cm" svg:height="29.713cm" draw:z-index="2">

and I want it to contain:

    <draw:frame draw:style-name="fr1" draw:name="Frame.3" text:anchor-type="paragraph" svg:width="21.001cm" svg:height="29.713cm" draw:z-index="2">

I have the feeling that I have to add anchortype="paragraph" or possibly SetAttribute('text:anchor-type', 'paragraph') somewhere in this section:

    for filename in sorted(glob("pdfpage*.png")):
        img = Image.open(filename)
        x,y=img.size
        cmx=21
        cmy=y*cmx/x
        img.close()
        doc.addImage(filename, filename)
        p = P(stylename="Illustration")
        p.addElement(doc.image(filename, cmx,cmy))
        doc.insertInCursor(p, after=True)
        if args.tesseract==True:
            for line in open(filename[:-4] +".txt", "r", encoding='UTF-8').readlines():
                p=P(stylename="Standard")
                p.addText(line)
                doc.insertInCursor(p, after=True)
    doc.save()

But I get errors like these:

TypeError: ODT.image() got an unexpected keyword argument 'anchortype'
AttributeError: Attribute text:anchor-type is not allowed in <draw:frame>

How do I find out how and where to add this?

1

There are 1 best solutions below

0
Ole Tange On

Replace

    p = P(stylename="Illustration")
    p.addElement(doc.image(filename, cmx,cmy))

with:

        p = P(stylename="Illustration")
        di=doc.image(filename, cmx, cmy)
        di.setAttribute(u'anchortype', u'paragraph')
        p.addElement(di)