[GIS] Loading print layout using an existing template (.qpt) with PyQgis3

pyqgis-3qgis-print-layouts

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?

Best Answer

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()

Related Question