7

I'm a real beginner in Python. I'm using a Python script to produce maps automatically.

This script is working but it is failing in the last part: saving the current map with a specific print layout. Im using the "Maps Printer" Plugin to do this.

The code bellow worked in QGIS 2.18 but fail in Qgis 3.8.

# Your settings
composerTitle = 'Composer_001' # Name of the composer you want to export
folder = 'C:/Users/.../'
extension = '.jpeg' # Any extension supported by the plugin
titre = 'NomCarte'

mp = qgis.utils.plugins['MapsPrinter']

for composer in iface.activeComposers():
    title = composer.composerWindow().windowTitle()
    if title == composerTitle:
        mp.exportCompo( composer, folder, titre, extension )
        break

The command iface.activeComposers seems to not work anymore in QGIS 3.0. Does anyone have an idea to solve this problem?

TomazicM
  • 25,601
  • 22
  • 29
  • 39
Yanuko
  • 83
  • 5

2 Answers2

6

You don't need loop active composer,only select print layout by name.

composerTitle = 'Composer_001' # Name of the composer you want to export

project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
layout = projectLayoutManager.layoutByName(composerTitle)
Fran Raga
  • 7,838
  • 3
  • 26
  • 47
1

Thank you so much Fran Raga, you solve my problem.

Here is the code to export a map by code using Maps Printer plugin :

from qgis.core import *
import qgis.utils

composerTitle = 'Composer_001' # Name of the composer you want to export
folder = 'C:/Users/.../'
extension = '.jpeg' # Any extension supp
title = 'TestExport'
project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
layout = projectLayoutManager.layoutByName(composerTitle)

mp = qgis.utils.plugins['MapsPrinter']
mp.exportCompo(layout, folder, title, extension)
Yanuko
  • 83
  • 5