Menyimpan ini dalam file Python dan menggunakannya dalam --code arg harus melakukan apa yang Anda butuhkan:
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()
Ini akan mengulang semua komposer dan mencetaknya jika atlasnya diaktifkan menjadi satu PDF untuk masing-masing komposer.
CATATAN Anda dapat membuka file proyek dalam sesi QGIS menggunakan iface.addProject
. Jadi Anda bisa melakukan ini untuk menghindari membuka banyak sesi QGIS.
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()
sys.exit()
untuk menutup QGIS saat selesai.QgsApplication.exit()
iface.actionExit()
akan menampilkan dialog jika proyek telah berubah dan memblokir pintu keluar, vsQgsApplication.exit()
yang hanya akan membatalkan proses.