9

I have a large project which consists of around 10x QGIS project files. Each project contains a print composer with an atlas set up to generate up to 10 maps.

What is the easiest way to automate the opening of each project and creating a PDF using the composer atlas?

(Something like this would be ideal - not sure if it's possible)

C:\OSGeo4W\bin\qgis.bat --project MyProject1.qgs --code SaveAtlasAsPdf.py C:\OSGeo4W\bin\qgis.bat --project MyProject2.qgs --code SaveAtlasAsPdf.py

9

Saving this in a Python file and using it in the --code arg should do what you need:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

for comp in iface.activeComposers():
    print comp
    folder = r"C:\temp"
    title = "testing"
    printer = QPrinter()
    painter = QPainter()
    comp.composition().setUseAdvancedEffects(False)
    myAtlas = comp.composition().atlasComposition()

    if myAtlas.enabled():
        myAtlas.beginRender()
        comp.composition().setAtlasMode(QgsComposition.ExportAtlas)
        myAtlas.setFilenamePattern(u"'{}_'||$feature".format(title))
        for i in range(0, myAtlas.numFeatures()):
            myAtlas.prepareForFeature(i)
            filename = os.path.join(folder, title + '.pdf')
            print filename
            comp.composition().beginPrintAsPDF(printer, filename)
            comp.composition().beginPrint(printer)
            printReady = painter.begin(printer)
            if i > 0:
                printer.newPage()
            comp.composition().doPrint(printer, painter)
        myAtlas.endRender()
        painter.end()

This will loop all composers and print them if they have atlas enabled into a single PDF for each one.

NOTE You can open a project file in a QGIS session using iface.addProject. So you could do this to avoid opening many QGIS sessions.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

def print_it():
    for comp in iface.activeComposers():
        print comp
        folder = r"C:\temp"
        title = "testing"
        printer = QPrinter()
        painter = QPainter()
        comp.composition().setUseAdvancedEffects(False)
        myAtlas = comp.composition().atlasComposition()

        if myAtlas.enabled():
            myAtlas.beginRender()
            comp.composition().setAtlasMode(QgsComposition.ExportAtlas)
            myAtlas.setFilenamePattern(u"'{}_'||$feature".format(title))
            for i in range(0, myAtlas.numFeatures()):
                myAtlas.prepareForFeature(i)
                filename = os.path.join(folder, title + '.pdf')
                print filename
                comp.composition().beginPrintAsPDF(printer, filename)
                comp.composition().beginPrint(printer)
                printReady = painter.begin(printer)
                if i > 0:
                    printer.newPage()
                comp.composition().doPrint(printer, painter)
            myAtlas.endRender()
            painter.end()

for project in projectlist:
    iface.addProject(project)
    print_it()
  • Relevant post and great answer! – CARTOS Jan 31 '16 at 16:12
  • I will assume it worked? You can use sys.exit() to close QGIS when done. – Nathan W Feb 2 '16 at 1:42
  • Yes, it works perfectly. I was actually trying to find a way to exit. I tried iface.actionExit().trigger() and sys.exit() but for some reason they're not working. – Damien Feb 2 '16 at 2:04
  • Try QgsApplication.exit() – Nathan W Feb 2 '16 at 2:33
  • iface.actionExit() is going to show a dialog if the project has changed and block the exit, vs QgsApplication.exit() which will just nuke the process. – Nathan W Feb 2 '16 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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