I am a newbie to QGIS Python. I have a shape-file which I want to load to QGIS 2.8.1 and manipulate them by zooming to the layer of the preferred shape-file.

Once I have my canvas ready, I want to use that canvas in the composer manager and export it to a png file.

Below is my script:

import ogr,os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
import qgis.utils
import glob
import time
import math
import processing
from processing.core.Processing import Processing
from PyQt4.QtCore import QTimer
import sys

OutputFileName = "ABC"    # Temprory global placeholder for filename
canvas = iface.mapCanvas()

#Add List of address below for heatmap shapefile
heatmapfile = ["C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/Map_00721.shp"]

# Add array of address below for  company shapefile
cspFile = ["C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/HM_13_June_19.shp"]

for lop in range(filesLen):
    flag = 0
    currentShpFile = heatmapfile[lop]
    currentCompShpFile = cspFile[lop]

    #enter the common path for each shapefile here
    currentShpFileName = currentShpFile.strip("C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/")
    #enter the common path for each shapefile here
    currentCompShpFileName = currentCompShpFile.strip("C:/Users/prashant.kumar/Desktop/QGIS python/Shapefiles/")

    # enter the output path here
    OutputFileName = "C:/Users/prashant.kumar/Desktop/QGIS python/ImageOut/" + currentShpFileName + ".png"

    # adding the shapefiles, changing the orders and zoom to layer
    wbc = QgsVectorLayer(currentCompShpFile, currentCompShpFileName, 'ogr')
    wb = QgsVectorLayer(currentShpFile, currentShpFileName, 'ogr')
    wbTex = QgsVectorLayer(TexLayer, 'CountyGrid', 'ogr')
    QgsMapLayerRegistry.instance().addMapLayer(wbc)    # Add the shapefile
    QgsMapLayerRegistry.instance().addMapLayer(wb)    # Add the shapefile
    QgsMapLayerRegistry.instance().addMapLayer(wbTex)    # Add the county shapefile
    qgis.utils.iface.setActiveLayer(wbc)   # Makes wb as active shapefile
    qgis.utils.iface.zoomToActiveLayer()    #Zoom to Layer
    print "Ready for composer manager"

    #Starting the canvas manager
    comp = iface.createNewComposer('currentcomposer')

Now I need help so I can use the current state of my canvas to be in the composer manager and export it as a PNG file.

  • Is there a specific reason you're using an outdated QGIS-version?
    – Erik
    Oct 25, 2019 at 11:31
  • Yes @Erik , I can't update to the new one for some admin related reasons. :( Oct 25, 2019 at 11:36
  • But your admin could update the software, I guess? I am asking, because finding an answer is way easier when you use an up to date version.
    – Erik
    Oct 25, 2019 at 11:36
  • Yes, but then he would have to do the same for everyone in our organisation Oct 25, 2019 at 11:37
  • Usually there's remote maintenance software in place in order to ease these global update processes. If there isn't, it isn't your problem.
    – Erik
    Oct 25, 2019 at 11:39

1 Answer 1


I found the answer here. The script worked perfectly. Please follow the link to check the proper method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.