QGIS - How to export Atlas, separate pdfs?

Viewed 766

I have used the code from the following post https://gis.stackexchange.com/questions/272839/export-a-configured-atlas-with-a-python-script-command-line to export the QGIS Atlas to png. It works fine, I get a separate png for every feature.

from qgis.core import  QgsApplication, QgsProject, QgsLayoutExporter
import os

def export_atlas(qgs_project_path, layout_name, outputs_folder):

    # Open existing project
    project = QgsProject.instance()
    project.read(qgs_project_path)

    print(f'Project in "{project.fileName()} loaded successfully')

    # Open prepared layout that as atlas enabled and set
    layout = project.layoutManager().layoutByName(layout_name)

    # Export atlas
    exporter = QgsLayoutExporter(layout)
    settings = QgsLayoutExporter.ImageExportSettings()
    exporter.exportToImage(layout.atlas(),outputs_folder, 'png', settings)


def main():
    # Start a QGIS application without GUI
    qgs = QgsApplication([], False)
    qgs.initQgis()

    project_path = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\print_test_3_14.qgz'
    output_folder = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\Output\\'
    layout_name = 'Photoviewer_wildlife'

    export_atlas(project_path, layout_name, output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

I am trying to get the same for a pdfs (a single pdf for each feature) however I am struggling with the code due to limited python knowledge. This will create a pdf but with no filename and its not picked up the first atlas feature.

import os

def export_atlas(qgs_project_path, layout_name, outputs_folder):

    # Open existing project
    project = QgsProject.instance()
    project.read(qgs_project_path)

    print(f'Project in "{project.fileName()} loaded successfully')

    # Open prepared layout that has atlas enabled and set
    layout = project.layoutManager().layoutByName(layout_name)

    # Export atlas
    exporter = QgsLayoutExporter(layout)
       exporter.exportToPdf('D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\Output\\'+layout.atlas().currentFilename()+".pdf", QgsLayoutExporter.PdfExportSettings())

def main():
    # Start a QGIS application without GUI
    qgs = QgsApplication([], False)
    qgs.initQgis()

    project_path = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\print_test_3_14.qgz'
    output_folder = 'D:\\DATA\\GIS\\QGIS\\workspaces\\do_not_use\\Print\\Output\\'
    layout_name = 'Photoviewer_wildlife'

    export_atlas(project_path, layout_name, output_folder)

    # Close the QGIS application
    qgs.exitQgis()

if __name__ == "__main__":
    main()

Any assistance would be really appreciated.

1 Answers

You need to loop through the atlas's layouts. I'm using something like this

def print_atlas_as_individual_pdfs(layout,savepath):
    my_atlas = layout.atlas()
    #this is needed if running directly from python
    #i don't think it's necessary if run from within qgis
    my_atlas.beginRender()
    while my_atlas.next():
        pdfpath = getpdfpath(savepath,my_atlas.currentFilename())
        atlas_layout=my_atlas.layout()
        exporter = QgsLayoutExporter(atlas_layout)
        exporter.exportToPdf(pdfpath,exporter.PdfExportSettings())
Related