0

I have defined an HTML Frame in my composer using:

html_frame = composition.getComposerItemById('html_frame')

How can I set its source using PyQGIS?


enter image description here

4

1 Answer 1

1

Add a sample code using a label and html:

from PyQt4.QtGui import QFont

composition = iface.activeComposers()[0].composition()
html_frame = composition.getComposerItemById('html')
composer_html = html_frame.multiFrame()
label = composition.getComposerItemById('label')

#Html Example
output_html = "<b>Example! From Code 2</b>"
composer_html.setHtml(output_html)
composer_html.loadHtml()

#Label Example
label.setText("Hello, World! From Code")
label.setFont(QFont("Cambria",15, QFont.Bold))
label.adjustSizeToText()

composition.refreshItems()

But if you want only change Html Frame only is necessary this 6 lines:

#Html Example
composition = iface.activeComposers()[0].composition()
html_frame = composition.getComposerItemById('html')
composer_html = html_frame.multiFrame()
output_html = "<b>Example! From Code</b>"
composer_html.setHtml(output_html)
composer_html.loadHtml()

regards

1
  • 1
    Absolutely perfect! I also included composer_html.setContentMode(1) which sets the HTML Source parameter to Source instead of the default URL (which was needed for my case as I am creating an HTML frame from scratch). Many thanks =)
    – Joseph
    Commented Feb 20, 2018 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.