I am trying to print a pdf map, while using a .qpt template.
I can create a pdf output but I how do I name the created print composer?
Each time I run the tool it creates a new 'Composer 1' then 'Composer 2'... and I need to reference the name of the composer in the print to pdf code for it to work (Composer 14 below).
This is my current code:
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* # Pan to selected feature pan_feature = iface.mapCanvas().panToSelected() #Next Set print composer to map canvas using python: canvas = qgis.utils.iface.mapCanvas() layers = canvas.layers() myFile = '/Users/Michael/QGIS_template.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('Map') map_item.setMapCanvas(canvas) map_item.zoomToExtent(canvas.extent()) # Next Print PDF using the printer plugin: composertitle = 'Composer 14' folder = '/Users/Michael/Capstone' extension = '.pdf' mp = qgis.utils.plugins ['MapsPrinter'] for composer in iface.activeComposers(): title = composer.composerWindow().windowTitle() if title == composertitle: mp.exportCompo (composer, folder, title, extension) break
My plugin is useless if the composer Id need to be updated every time I run the tool.
These are the two main Q&As that I used to build my code above: