4

I want to add layout number to each layout in the print composer. I've cant find a variable with that information in it that I can access from the expression builder.

Generally our projects are built with many single page layouts. They need a unique ID per project, but this ID can overlap with other projects. So I figured we could automate away the task of making sure each layout has a separate ID number printed on it, only I can't figure a way to get at the data within the expression builder.

Does anyone have an idea on a way to get a unique ID onto each print layout? (would prefer an incriminating one like 0,1,2,3,4)

1 Answer 1

7

you can make your own expression for that with the function editor:

enter image description here

and then use the new expression in the layout:

enter image description here

with the following code:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def layoutid(layoutname, feature, parent):
    """
    Returns a unique layout id.
    <h2>Example usage:</h2>
    <ul>
      <li>layoutid(@layout_name)
    </ul>    
    """
    manager = QgsProject.instance().layoutManager()
    layoutlist = manager.layouts()
    id = 1
    for layout in layoutlist:
        if layout.name()==layoutname:
            break
        id +=1
    return id

Your Answer

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

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