Python Forum
Python QGIS tool that replaces layout text labels with attributes from an input table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python QGIS tool that replaces layout text labels with attributes from an input table
#1
Hello everyone,

I'm writing a QGIS tool and I think I got it but it doesn't work. Maybe someone can help me.

In the Qgs Project is a .csv table file with two fields and two features/Objects.

The script I'm writing is supposed to replace two textlabels in my open layout with the attributes in the second field of the table.

My script has the following procedure:

1) take the table from the project and store it in a variable

2) take the text labels from the layout and store it in a variable

3) compare the text labels with the attributes from the first field and if they are equal overwrite the text label with the content from the second field.

My script doesn't give me an error message but it is not doing anything. apparently I missed something but I don't see what it is. Hoping for help.

Here is the code:
     def getTable(self):
        """gets table from the QGIS Project"""
        layer = None
        for lyr in QgsProject.instance().mapLayers().values():
            if lyr.name() == "table":
                layer = lyr
                break
        return layer

    def setVariable(self):
        """adds the table to a variable called self.inTable"""
        self.inTable = self.getTable()

    def changeLabel(self):

        """saves the Layout label texts in a variable"""
        labels = None
        for l in QgsProject.instance().layoutManager().layouts()[0].items():
            if isinstance(l,QgsLayoutItemLabel):
                labels = l

        """gets the features of the table"""
        features = self.inTable.getFeatures()

        """compares the list of labels with the attributes in the first 
        column of the features and replaces the label with the attribute
        from the second column"""
        for label in labels:
            for feat in features:
                if label == feat.attributes()[0]:
                    labels[label] = feat.attributes()[1]
        return labels

    def run(self):
        """Run method that performs all the real work"""
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            self.setVariable()
            self.changeLabel()
 
I guess there is something wrong in the changeLabel function but I don't see what it is. Or I completely forgot something. Perhaps my question is too specific for this forum but I thought maybe someone can help.
Reply
#2
Just a guess, but I would expect that QgsProject.instance().layoutManager().layouts()[0].items() would return some kind of iterable of 2-element tuples (e.g. like dict.items()) in this case l would not be an instance of QgsLayoutItemLabel.
As a side note PEP8 recommendation:
Quote:Names to Avoid

Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.

In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use 'l', use 'L' instead.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QGIS 3.34 Python Error paskiller 2 605 Nov-13-2023, 06:20 PM
Last Post: buran
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Color a table cell based on specific text Creepy 11 1,826 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Use module docx to get text from a file with a table Pedroski55 8 5,795 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  Is there software/tool for coding Python? dee 11 2,748 Jun-14-2022, 02:32 PM
Last Post: dee
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,584 May-27-2022, 04:59 PM
Last Post: Winfried
  Help adding prompt text in a Layout using Rich TUI Extra 2 1,570 May-23-2022, 07:15 PM
Last Post: Extra
  How to perform DESC table sort on dates stored as TEXT type. hammer 7 2,127 Mar-15-2022, 01:10 PM
Last Post: hammer
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,873 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  python tool to collect the time measurement of entire code maiya 3 2,237 Feb-12-2021, 05:39 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020