I want to create a atlas composer map using PyQGIS3. How can I take dynamic path by field from atlas layer?

enter image description here enter image description here

In PyQGIS3 I need some like this :

pict = QgsLayoutItemPicture(layout)

where myAtlas is my layer and pict is my field with paths.


For the expression, you should use the following

  file_exists(attribute(@atlas_feature, 'pict')),
  attribute(@atlas_feature, 'pict'),

For manipulating expressions, you should use

from qgis.core import QgsProject, QgsLayoutItemPicture

project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('atlas')

# By default, when you create any component in the layout component, no Item ID, so set it before
item = layout.itemById('theidoftheQgsLayoutItemPicture')
your_atlas_field = 'pict'
expression = "if(\n  file_exists(attribute(@atlas_feature, '{field}')),\n  attribute(@atlas_feature, '{field}'),\n  '/tmp/fallback.png'\n)".format(field=your_atlas_field)
  • attribute from myAtlas layers ? – Mar May 23 '20 at 1:10
  • If you generate an atlas, the expression enables you to get the atlas attribute for the current feature of the atlas. It's not what you are asking? – ThomasG77 May 23 '20 at 1:24
  • Confused, in QGIS term, there is a layout. Within this layout, you can set an atlas by ticking "Generate an atlas" and choose a layer called "Coverage layer". Where does myAtlas is coming from? In a QGIS atlas (the functionnality, not speaking about the everyday term), you have multiple layers and you loop from the coverage layer. – ThomasG77 May 23 '20 at 1:31
  • Your variable myAtlas seems to contains multiple layers whereas I expect you use only one layer: the coverage layer. It's what I mean by being confused. – ThomasG77 May 23 '20 at 1:34
  • yes i have generate atlas layers and working fine,my atlas in my coverange layer. myAtlas.setCoverageLayer(layer) coverage = project.layerStore().mapLayersByName("name")[0] myAtlas.setCoverageLayer(coverage) myAtlas.enabled() – Mar May 23 '20 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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