3

I'm trying to load a print layout (.qpt) which I saved earlier via PyQgis.

I found this earlier entry, but it's for QGIS 2.x and thus doesn't work for QGIS3: Programmatically load composer from template and generate atlas using pyQgis

Does anyone know how to do this in QGIS3?

1 Answer 1

7

For load qpt in QGIS 3 you need something like this:

# Load template from file
p = QgsProject()
l = QgsLayout(p)
tmpfile = 'D://temp//test.qpt'
with open(tmpfile) as f:
    template_content = f.read()
doc = QDomDocument()
doc.setContent(template_content)

# adding to existing items
items, ok = l.loadFromTemplate(doc, QgsReadWriteContext(), False)

look at the API loadFromTemplate()

1
  • May need to use 'QgsPrintLayout' instead of 'QgsLayout' now (for some applications)
    – cefect
    Feb 9, 2022 at 19:36

Your Answer

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

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