I have multiple maps generated in a pyqgis script. Those maps I want to export them automatically with different aspect ratios in order to be printable on different canvases.

This is the list of aspect ratios:

    aspect_ratios = [
        (1, 1),           # 1:1 (Square)
        (4, 3),           # 4:3
        (3, 2),           # 3:2
        (16, 9),          # 16:9
        (5, 4),           # 5:4
        (2, 1),           # 2:1 (Panoramic)
        (1618, 1000),     # Golden Ratio (1.618:1)
        (2, 3),           # 2:3
        (3, 4),           # 3:4
        (4, 5),           # 4:5
        (11, 14),         # 11:14
        (1414, 1000),     # International Ratios (ISO Paper Sizes): A1
        (1189, 1000),     # International Ratios (ISO Paper Sizes): A2
        (1414, 1000),     # International Ratios (ISO Paper Sizes): A3
        (1000, 1414),     # International Ratios (ISO Paper Sizes): A4
        (1414, 2000)      # International Ratios (ISO Paper Sizes): A5

As far as I know, I need to set the layout page size based on my aspect ratio list, within a "for" statement, as well as the map item extent should match the new layout page extent.

These are the layout and map item:

        layout = project.layoutManager().layoutByName('Test')
        map_item = layout.itemById('Map 1')

The map Item I think it can be solved by this:

        for aspect_ratio in aspect_ratios: 

            map_item = layout.itemById('Map 1')
            map_item.setRect(QRectF(0, 0, aspect_ratio[0], aspect_ratio[1]))

The layout page size I have tried to update it like this: layout_width = width * 100 layout_height = height * 100 layout.setFixedSize(layout_width, layout_height)

Then I save all the different aspect ratio images into a specific folder:

            jpeg_path = os.path.join(directory, city_name, f"{city_name}_{aspect_ratio[0]}_{aspect_ratio[1]}.jpeg")
            exporter = QgsLayoutExporter(layout)
            settings = QgsLayoutExporter.ImageExportSettings()

            #settings.cropToContents = True # face crop la orice ii in jurul la map item
            settings.dpi = 1000

The main issue is that I have tried different ways to set a specific size for the layout page and also for the map item but none of the worked, and there is something that I am missing. Does anyone know any specific way to solve this?

1 Answer 1


You must get the page

project = QgsProject.instance()
layout = project.layoutManager().layoutByName('Test')
map_item = layout.itemById('Map 1')

page = NULL
for i,x in enumerate(layout.items()):
    if isinstance(x, QgsLayoutItemPage):
        page = layout.items()[i]
aspect_ratios = [(100, 100)]

layout_size = QgsLayoutSize(aspect_ratios[0][0], aspect_ratios[0][1], QgsUnitTypes.LayoutMillimeters)


But I think you should revisit the hadnling aspect_ratio, page width and dpi.But perhaps i just missed something. You can use QgsUnitTypes.LayoutPixels instead of QgsUnitTypes.LayoutMillimeters.

Perhaps the center point and the mapscale is although an aspect.

  • Indeed page.setPageSize(layout_size) works. Now I still need to figure it out how to change the same for map item in order to fit properly and the shapefiles presented in that map item to "stretch" perfectly and fit into that map item. Also I will need to anchor other map items such as title scale and north arrow. But that would be another topic of discussion. Thank you! Commented Feb 16 at 19:53
  • Solved also the map item to have the same size as layout: # Create QgsLayoutSize for map item size map_size = QgsLayoutSize(page_width, page_height) # Set map item size map_item.attemptResize(map_size) Commented Apr 20 at 13:59

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.