Looking for a solution to loop through existing layouts in a QGIS 3.6 document and change a source of an image. The image is the same in each of the layouts. What I have is:

from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer, 
import os,sys
root = QgsProject.instance().layerTreeRoot()
project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
for layout in projectLayoutManager.layouts():
    #print (layout)
    image = layout.itemById("footer")
    print (image)

It seems to find the image just fine. Returns:

<qgis._core.QgsLayoutItemPicture object at 0x0000016071A895E8>

but fails to do anything from the last line:

AttributeError: 'NoneType' object has no attribute 'setPicturePath'
  • 1
    Perhaps there's an Item ID that is empty in one of your layouts? Otherwise your code seems to work fine, at least on QGIS 3.8. – Joseph Nov 7 '19 at 12:01
  • @Joseph You are right. There were some layout in the mix without the image. The rest of the layouts worked fine. Thanks – Jan Nov 23 '19 at 3:57

Your Answer

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

Browse other questions tagged or ask your own question.