I have a QGIS v2.18 project that includes a print composer layout. In the print composer I can alter the exent to match the canvas extent using the Set to Map Canvas extent button. I would like to move the extent of the display in the canvas area manually, save the QGIS project, and run code that will change the extent of the print composer to match the extent in the map view area. I have found this code on GIS Stack Exchange and have altered it for my data (shown below). After running this code in the Python console this only creates a duplicate the original composer and the original composer's extent. The code does not alter the extent to match the current canvas area. How do alter this code so I can change the view in the map viewing area, and run Python code, and force the print composer to match the extent of the main view area?
import os 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 = "Z:\GISpublic\GerryG\QGIS\mainmap.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.composition().getComposerItemById('mainmap') map_item.setMapCanvas(canvas) map_item.zoomToExtent(canvas.extent())