2

I have this function works totally fine. But i want to be able to change the font size

 def label(wah, field):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == wah:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = "["+field+"]"
                    lyr.showLabels = True

I have tried something like this but i cannot quite seem to get it to work. The label size is not changing from the code below

 lblClass.expression = "<FNT size = '8'>" + "[" + {} + "]" + "</FNT>".format(field)

or

lblClass.expression = '"{}" + [{}] +  "{}"'.format("<FNT size = '8'>",field,"</FNT>")

Just for reference when i am calling the function it looks like this:

label("CountyClip", "County")

Can someone help me fix my code?

1
  • 2
    What about: "<FNT size = '8'>[{0}]</FNT>".format(field) ? Commented May 26, 2016 at 18:01

1 Answer 1

3

You've got a little tied up constructing your string

def label(wah, field):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == wah:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = "<FNT size='8'>[{}]</FNT>".format(field)
                    lyr.showLabels = True

You don't need to concatenate the string together using + when you're using the str.format(). Construct your entire string as-is, and use {} as the place-holder for input from the str.format() such as your field value.

And if you still have trouble with this you could enclose the string in triple quotes """ as this can help get around any potential escape character issues. e.g.

lblClass.expression = """<FNT size='8'>[{}]</FNT>""".format(field)
0

Your Answer

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

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