I have a QGIS python script which successfully loads a specified print composer template.
At the end of this script I have attempted to set the composer to the map canvas extent, but I receive the following error message:
AttributeError: 'QgsComposerView' object has no attribute 'getComposerItemById'
I'm running Windows 10, QGIS 2.14.7
The code is as follows:
import os,sys
from qgis.core import*
from PyQt4.QtCore import*
from qgis.utils import*
from qgis.gui import *
import qgis
from PyQt4.QtXml import *
canvas = qgis.utils.iface.mapCanvas()
layers = canvas.layers()
myFile = os.path.join('P:\Office Admin\GIS\Templates\Plan templates\QGIS template plans\Title deed templates', 'A0_landscape_title deed.qpt')
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()
myDocument = QDomDocument()
myDocument.setContent(myTemplateContent, False)
comp = iface.createNewComposer()
comp.composition().loadFromTemplate(myDocument)
map_item = comp.getComposerItemById('Map 1')
map_item.setMapCanvas(canvas)
map_item.zoomToExtent(canvas.extent())
map_item = comp.getComposerItemById('Map 1')
withmap_item = comp.composition().getComposerItemById('Map 1')