Skip to content

Instantly share code, notes, and snippets.

@timlinux
Created February 17, 2014 07:00
Show Gist options
  • Save timlinux/9045978 to your computer and use it in GitHub Desktop.
Save timlinux/9045978 to your computer and use it in GitHub Desktop.
from qgis.core import QgsComposition
from PyQt4 import QtCore, QtXml
path = 'c:\test.qpt'
template_file = QtCore.QFile(path)
template_file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
template_content = template_file.readAll()
template_file.close()
document = QtXml.QDomDocument()
document.setContent(template_content)
canvas = iface.mapCanvas()
renderer = canvas.mapRenderer()
load_ok = self.composition.loadFromTemplate(document)
composition = QgsComposition(renderer)
composition.setPlotStyle(QgsComposition.Print) # or preview
composition.setPrintResolution(300)
composition.setPrintAsRaster(True)
composer = iface.createNewComposer()
composer.setComposition(composition)
@akbargumbira
Copy link

from qgis.core import QgsComposition
from PyQt4 import QtCore, QtXml

path = 'c:/temp/test.qpt'
template_file = QtCore.QFile(path)
template_file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
template_content = template_file.readAll()
template_file.close()
document = QtXml.QDomDocument()
document.setContent(template_content)

canvas = iface.mapCanvas()
renderer = canvas.mapRenderer()
composition = QgsComposition(renderer)
composition.setPlotStyle(QgsComposition.Print) # or preview
composition.setPrintResolution(300)
composition.setPrintAsRaster(True)
composition.loadFromTemplate(document)

composer = iface.createNewComposer()
composer.setComposition(composition)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment