Skip to content

Instantly share code, notes, and snippets.

@kikislater
Created April 15, 2019 15:22
Show Gist options
  • Save kikislater/185fe92fb3add1add962cd68077ed0d9 to your computer and use it in GitHub Desktop.
Save kikislater/185fe92fb3add1add962cd68077ed0d9 to your computer and use it in GitHub Desktop.
Export QGIS 3 layout from pyqgis 3 script
#!/usr/bin/env python3
import os
from qgis.core import (QgsProject, QgsLayoutExporter, QgsApplication)
QgsApplication.setPrefixPath("/usr", True)
gui_flag = False
app = QgsApplication([], gui_flag)
app.initQgis()
project_path = os.getcwd() + '/project.qgz'
project_instance = QgsProject.instance()
project_instance.setFileName(project_path)
project_instance.read()
manager = QgsProject.instance().layoutManager()
layout = manager.layoutByName("my_layout") # name of the layout
# or layout = manager.layouts()[0] # first layout
exporter = QgsLayoutExporter(layout)
exporter.exportToPdf(project_instance.absolutePath() + "/layout.pdf",
QgsLayoutExporter.PdfExportSettings())
app.exitQgis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment