DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

12- Map Printing with PyQGIS

Layout.png

Learn how to easily print maps using PyQGIS with this step-by-step guide. PyQGIS is a Python library that provides access to QGIS API, allowing you to automate tasks and create custom plugins. In this tutorial, we'll use QgsLayoutExporter() class to export layouts as images, PDFs, and SVGs. Whether you're a GIS professional or a Python developer, you'll find this tutorial helpful to enhance your mapping skills.

We use the QgsLayoutExporter() class to export a layout that we opened:

manager = QgsProject.instance().layoutManager()
print(manager.printLayouts())

layout = manager.layoutByName("Layout1")
Enter fullscreen mode Exit fullscreen mode

We use the QgsLayoutExporter() class to export to image, SVG, and PDF:

exporter = QgsLayoutExporter(layout)
Enter fullscreen mode Exit fullscreen mode

Export to PNG image:

exporter.exportToImage("D:/Python_QGIS/Layout1.png", QgsLayoutExporter.ImageExportSettings())
Enter fullscreen mode Exit fullscreen mode

Layout1.png

Export to PDF:

exporter.exportToPdf("D:/Python_QGIS/Layout1.pdf", QgsLayoutExporter.PdfExportSettings())
Enter fullscreen mode Exit fullscreen mode

Export to SVG image:

exporter.exportToSvg("D:/Python_QGIS/Layout1.svg", QgsLayoutExporter.SvgExportSettings())
Enter fullscreen mode Exit fullscreen mode

Use a for loop to export the layout:

for layout in manager.printLayouts():
    exporter = QgsLayoutExporter(layout)
    exporter.exportToImage("D:/Python_QGIS/Image2.png".format(layout.name()),
        QgsLayoutExporter.ImageExportSettings())
Enter fullscreen mode Exit fullscreen mode

In conclusion, this tutorial provides a step-by-step guide on how to easily print maps using PyQGIS. It explains how to use QgsLayoutExporter() class to export layouts as images, PDFs, and SVGs. By following this tutorial, GIS professionals and Python developers can enhance their mapping skills and automate tasks. Overall, this tutorial is a helpful resource for anyone looking to improve their knowledge of map printing with PyQGIS.

If you like the content, please SUBSCRIBE to my channel for the future content

Oldest comments (0)