1

What I am hoping to be able to achieve with this Tool within my Toolbox is to allow the user to input a number of layers and to set all of the labels with the same font settings (font style, color, size, etc.). I started with the font size. Within my getParameterInfo() function, I have defined the following parameters:

        updlayers = arcpy.Parameter(
        displayName = "Layers with features to be updated",
        name = "updlayers",
        datatype = "GPValueTable",
        parameterType = "Required",
        direction = "Input")

        labelFontSize = arcpy.Parameter(
        displayName = "Label Font Size",
        name = "labelFontSize",
        datatype = "GPDouble",
        parameterType = "Required",
        direction = "Input")

Then, within my execute function, I have the following pieces of code:

 updlayers = parameters[1].values
 labelFontSize = parameters[2].value

 for layers in updlayers:
        for labels in layers:
            labels.showLabels = True
            for a in labels.labelClasses:
                    a.expression = '"%s" + [Name] + "%s"' %  ("<BOL><FNT name='Arial' size='labelFontSize'>","</FNT></BOL>")

I know that my code works fine if I replace 'labelFontSize' to an actual numerical value. I also attempted to change my expression to instead use labelFontSize as a string:

a.expression = '"%s" + [Name] + "%s"' %  ("<BOL><FNT name='Arial' size=str(labelFontSize)>","</FNT></BOL>")

However, for some reason, the label either doesn't appear at all or it shows the actual expression text as my label. The issue I am having with is obviously within the expression part of my code.

However, how do I apply my labelFontSize variable into this expression?

Am I going the correct route with this or is there an easier way to do this?

1

It looks like you are placing your variable labelFontSize inside a string? I would have thought this line:

a.expression = '"%s" + [Name] + "%s"' %  ("<BOL><FNT name='Arial' size='labelFontSize'>","</FNT></BOL>")

would have been something like (I've not tested this!):

a.expression = '"%s" + [Name] + "%s"' %  ("<BOL><FNT name='Arial' size='" + str(labelFontSize) + "'>","</FNT></BOL>")
  • You're absolutely right! That actually seems to resolve the issue. Thanks much, I appreciate it. Is there any reason why you have a single quote followed by a double quote when inputting labelFontSize? As in, the code is essentially: ' "labelFontSize" ' – coolDude Sep 30 '14 at 23:07

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.