自动化QGIS项目编辑器打印?


9

我有一个大型项目,包含大约10个QGIS项目文件。每个项目都包含一个打印作曲家,该作曲家设置了可生成多达10张地图的地图集。

使用作曲者地图集自动打开每个项目并创建PDF的最简单方法是什么?

(像这样的东西是理想的-不确定是否可能)

C:\ OSGeo4W \ bin \ qgis.bat-项目MyProject1.qgs-代码SaveAtlasAsPdf.py C:\ OSGeo4W \ bin \ qgis.bat-项目MyProject2.qgs-代码SaveAtlasAsPdf.py

Answers:


9

将其保存在Python文件中并在--code arg中使用它应该可以满足您的需要:

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()

如果所有的作曲家都启用了图集,则这将循环所有作曲家并将其打印为一个PDF。

注意您可以使用在QGIS会话中打开项目文件iface.addProject。因此,您可以这样做以避免打开许多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()用来关闭QGIS。
内森·W

是的,它运作良好。我实际上是试图找到一种方式来退出。我尝试了iface.actionExit()。trigger()和sys.exit(),但是由于某些原因,它们无法正常工作。
Damien

试试QgsApplication.exit()
Nathan W

iface.actionExit()如果项目已更改并阻止退出,QgsApplication.exit()则会显示一个对话框,而这只会取消该过程。
内森·W
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.