unable to add image to QTextDocument saved as .odt file

Roberta Fagandini robifagandini at gmail.com
Tue Aug 16 13:08:29 BST 2022


Hi all,
I have developed a QGIS python plugin whose output is an .odt file with
text and images. In order to create the .odt file I have written the
folowing code block:
'''
*html_text = '<!DOCTYPE html><html><head></head><body>\*
*<p>My text</p>\*
*<p style="text-align:center; vertical-align: middle;">\*

*<img src="path/to/image/logo.png"></p>\</body></html>'*


*doc = QTextDocument()doc.setHtml( html_text )*




*writer =
QTextDocumentWriter()writer.setFormat(QByteArray(b'ODF'))writer.setFileName('path/to/file/test.odt')writer.write(doc)*

'''
The code above  works perfectly till QGIS 3.16 version wich was compiled
with Qt 5.11. In the later QGIS versions (3.22 - 3.24 - 3.26) compiled with
Qt 5.15, the odt file is correctly created but the image is not added to
the file. If I change the file format, for instance
*writer.setFormat(QByteArray(b'HTML'))*, the image is added to the created
html file also with Qt 5.15.
I have the same problem using QTextCursor.insertImage():
'''








*doc = QtGui.QTextDocument()cursor =
QtGui.QTextCursor(doc)cursor.insertText('Hello
world')cursor.insertImage('path/to/image/logo.png')writer =
QtGui.QTextDocumentWriter()writer.setFormat( QByteArray(b'ODF')
)writer.setFileName(' path/to/file /test.odt')*
'''
The image is correctly added to the odt file with Qt 5.11 while it is not
added to the odt file with Qt 5.15.

Is this a bug or somenthing change in the QTextDocument
and QTextDocumentWriter classes? Looking at the documentation the html img
tag is still supported by the setHtml function (
https://doc.qt.io/qt-5/richtext-html-subset.html).

Any idea?

Thanks in advance!!
Roberta

-- 
*Arch. Roberta Fagandini, PhD*
*GIS and Remote Sensing Technician*
*Gter srl (Unige spin-off)*
*Via Jacopo Ruffini 9/1A - 16128 Genova*
*P.IVA/CF 01998770992*
*www.gter.it* <http://www.gter.it/>
*--*
*Gter social*
*www.twitter.com/Gteronline* <http://www.twitter.com/Gteronline>* - *
*www.facebook.com/Gteronline* <http://www.facebook.com/Gteronline>* -*
*www.linkedin.com/company/gter-srl-innovazione-in-geomatica-gnss-e-gis
<http://www.linkedin.com/company/gter-srl-innovazione-in-geomatica-gnss-e-gis>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20220816/bc4c59be/attachment.htm>


More information about the PyQt mailing list