2

I have a vector layer with an attribute text field with 3 words (word1, word2 and word3). I want to capitalize the first letter of word1 and word3 in label output. For example if a record has the following words "rio della casa" i want as label "Rio della Casa". I know in QGIS there is the title() statement but it capitalizes the first letter of all the words, so can someone help me to find the right rule for labelling?

6

You can create a custom function in the Field Calculator to capitalize the first letter of the first and last words. The following function can do what you are looking for:

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

@qgsfunction(args='auto', group='Custom')
def upper_word(text_field, feature, parent):
    split_txt = text_field.split()
    new_txt = split_txt[0].title() + ' ' + split_txt[1] + ' ' + split_txt[2].title()
    return new_txt

and here how it looks in the Field Calculator:

enter image description here

Then call the function from the custom mene:

enter image description here

Here is the output in the attribute table:

enter image description here

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.