How to create a QGIS PDF report with a few lines of python

Posted by: Gavin Fleming | in FOSSGIS, QGIS | 2 years, 3 months ago | Comments

Sometimes you want to automatically generate a report to reflect the latest state of your data. For example you may be capturing spatial data into a PostGIS database and want a snapshot of that every few hours expressed as a pdf report. This example shows you how you can quickly generate a pdf based on a QGIS project (.qgs file) and a QGIS template (.qpt file).

# coding=utf-8

# A simple demonstration of to generate a PDF using a QGIS project
# and a QGIS layout template.
# This code is public domain, use if for any purpose you see fit.
# Tim Sutton 2015

import sys
from qgis.core import (
    QgsProject, QgsComposition, QgsApplication, QgsProviderRegistry)
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from PyQt4.QtCore import QFileInfo
from PyQt4.QtXml import QDomDocument

gui_flag = True
app = QgsApplication(sys.argv, gui_flag)

# Make sure QGIS_PREFIX_PATH is set in your env if needed!

# Probably you want to tweak this
project_path = 'project.qgs'

# and this
template_path = 'template.qpt'

def make_pdf():
    canvas = QgsMapCanvas()
    # Load our project
    bridge = QgsLayerTreeMapCanvasBridge(
        QgsProject.instance().layerTreeRoot(), canvas)

    template_file = file(template_path)
    template_content =
    document = QDomDocument()
    composition = QgsComposition(canvas.mapSettings())
    # You can use this to replace any string like this [key]
    # in the template with a new value. e.g. to replace
    # [date] pass a map like this {'date': '1 Jan 2012'}
    substitution_map = {
        'DATE_TIME_START': 'foo',
        'DATE_TIME_END': 'bar'}
    composition.loadFromTemplate(document, substitution_map)
    # You must set the id in the template
    map_item = composition.getComposerItemById('map')
    # You must set the id in the template
    legend_item = composition.getComposerItemById('legend')


(See here for any updates we may publish for this example)

Using this approach you can generate all kinds of useful outputs without ever needing to open QGIS each time you generate the report. Simply create the needed project and template files and then run it like this:

Current rating: 3.6


Template by Blacktie Mezzanine theme by CodingHouse