I have a script that iterates through layer subgroups in QGIS 3.6, opens a layout with the with the same name as the subgroup and exports a PDF. What I am struggling with is to close the active layout window after the export. Any ideas or other ways of doing such task?
from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer
import os,sys
root = QgsProject.instance().layerTreeRoot()
project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
for child in root.children():
if isinstance(child, QgsLayerTreeGroup):
#print ("- group: " + child.name())
for subChild in child.children():
if isinstance (subChild, QgsLayerTreeGroup):
print ("- group: " + child.name() + "-subgroup: " + subChild.name())
#turn on subgroup
child.setItemVisibilityChecked(True)
subChild.setItemVisibilityChecked(True)
#open layout
l_out = QgsProject.instance().layoutManager().layoutByName(subChild.name())
iface.openLayoutDesigner(layout=l_out)
mapName= (subChild.name()+".pdf")
print (mapName)
base_path = (r"F:\Documents\PDF")
print (base_path)
pdf_path = os.path.join(base_path,mapName)
print (pdf_path)
#export layout
exporter = QgsLayoutExporter(l_out)
exporter.exportToPdf(pdf_path, QgsLayoutExporter.PdfExportSettings())
#close Layout
???
#turn off layer
subChild.setItemVisibilityChecked(False)