4

I would like to automate two steps in my QGIS Print Layout by using PyQGIS

I have my print layout already created, and I was trying to open it by this code:

composerTitle = 'Strand3'

project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
layout = projectLayoutManager.layoutByName(composerTitle)

which comes from this query:

How to select a specific print layout (by name) in QGIS 3?

The code didn't work as I wanted. I just wanted to load my print layout called "Strand3" from the layout manager. However is not as important as the stuff below, because I can load it once manually and keep it open.

Next, I would like to do an automatic Set Map Extent to Match Main Canvas Extent.

I found some solution here:

but it doesn't work, as they propose the definition of map rectangle. I simply need my map refreshed, what was considered here:

I would like to have this step done automatically as well as the last one - exporting map to PNG.

Since I've managed with setting the fixed DPI resolution for the map, I want to skip the window below:

enter image description here

when possible.

I found the code like this:

 .exportToImage(fn, QgsLayoutExporter.ImageExportSettings())
 exporter.exportToPdf(fn, QgsLayoutExporter.PdfExportSettings())

and solution like this:

but I don't know how to implement Qgis Layout Exporter.

Anyway my final code looks like this:

 comp = iface.createNewComposer()
 comp.composition().loadFromTemplate(myDocument)

 map_item = comp.composition().getComposerItemById('Strand3')
 map_item.setMapCanvas(canvas)
 map_item.zoomToExtent(canvas.extent())

 base_path = os.path.join(QgsProject.instance().homePath())
 png_path = os.path.join(base_path, ".png")

 exporter = QgsLayoutExporter(layout)
 exporter.exportToPdf(png_path, QgsLayoutExporter.ImageExportSettings())

and I am getting an error: 'QgisInterface' object has no attribute 'createNewComposer'.

How can I automate these 2 things in my QGIS print layout?

0

1 Answer 1

8

As Kadir Şahbaz commented, you are mixing QGIS 2 & 3 APIs. A couple of other things:

-You don't need to actually open your layout, you just need a reference to the layout object.

-If you want to export a png you should use exportToImage() not exportToPdf().

Try the example below which is working for me.

project = QgsProject().instance()
canvas = iface.mapCanvas()

layout = project.layoutManager().layoutByName('Strand3')

map_item = [i for i in layout.items() if isinstance(i, QgsLayoutItemMap)][0]
map_item.zoomToExtent(canvas.extent())

fileName = QFileDialog.getSaveFileName(None, 'Save File', '', filter='*.png')
if fileName:
    png_path = fileName[0]

exporter = QgsLayoutExporter(layout)
settings = QgsLayoutExporter.ImageExportSettings()
#The idea is that here you can change setting attributes e.g.
#settings.cropToContents = True
#settings.dpi = 150

result = exporter.exportToImage(png_path, settings)
print(result)#0 = Export was successful!
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.