I've managed to export several atlases automatically from QGIS with a Python and a .bat file, see Export several configured atlases with a Python script
I'm using QGIS 3.10.2 on a Windows 10 PC and I'm connecting to PostgreSQL 9.5.2.
The QGIS project (and layouts/atlases) only contains shapefiles and WMS.
When I try to do the same thing with layouts containing PostgreSQL views and tables, no JPEG files are created. May it be due to PostgreSQL connecting issues?
The username and password for the PostgreSQL/PostGIS connection is stored in the QGIS project (using Authentication manager when creating a new PostGIS connection). (In addition username and password are stored in pgpass.conf.)
I've tried the code from @Matthias Kuhn (@Matthias Kuhn ) (host, tablename, username etc. are not the real ones used in my actual code)
#!/usr/bin/env python # -*- coding: utf-8 -*- # linja over bruker vi for å kunne bruke æøå uten advarsler. import os from qgis.core import QgsApplication, QgsProject, QgsLayoutExporter, QgsDataSourceUri, QgsCredentials #from PyQt5.QtSql import QSqlDatabase # Initialize QGIS Application QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True) # # Start a QGIS application without GUI qgs = QgsApplication([], False) qgs.initQgis() uri = QgsDataSourceURI() # assign this information before you query the QgsCredentials data store uri.setConnection("HOST", "PORT", "DB_NAME", None, None) connInfo = uri.connectionInfo() (success, user, passwd) = QgsCredentials.instance().get(connInfo, None, None) if success: uri.setPassword(passwd) uri.setUsername(user) uri.setDataSource("SCHEMA", "TABLE_NAME", "the_geom") QgsVectorLayer(uri.uri(), "<name of view>", "postgres") project_path = 'C:\\adhoc\\lst/lstp.qgs' output_folder = 'C:\\adhoc\\lst\\n5g\\' layout_name = 'n5 NGO48 sone G' # Open existing project project = QgsProject.instance() project.read(project_path) print('Project ' + 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(),output_folder, 'jpg', settings) # Close the QGIS application qgs.exitQgis() I also had a shot at @J. Montecolo (@J. Montecolo) including connInf, but connection fails.
... from qgis.core import QgsApplication, QgsProject, QgsLayoutExporter, QgsDataSourceUri, QgsCredentials from PyQt5.QtSql import QSqlDatabase # Initialize QGIS Application QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True) # # Start a QGIS application without GUI qgs = QgsApplication([], False) qgs.initQgis() host = "HOST" port = 5432 database = "DB_NAME" db = QSqlDatabase.addDatabase("QPSQL") db.setHostName(host) db.setPort(port) db.setDatabaseName(database) uri = QgsDataSourceUri() # assign this information before you query the QgsCredentials data store uri.setConnection("HOST", "PORT", "DB_NAME", None, None) connInfo = uri.connectionInfo() (success, user, passwd) = QgsCredentials.instance().get(connInfo, None, None) if success: db.setUserName(username) db.setPassword(password) if db.open(): print("Connection successful !") else: print("Connection failed.") ... Is it possible to automatically export layouts containing PostgreSQL views/tables? If yes, how may I adjust the Python code?