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 only 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 somethin.