1

I am trying to show the labels on the map on one particular field (Candidate Code) and its a String value, and also trying to set the scale range and placement property on labels (show labels out beyond 1:1500000 and top center only) But I am not sure how to do it.

Syntax error is showing when defined the Find Labels

Below is my complete code:

import arcpy
from arcpy import env
import arcpy.mapping

# Set environment settings
env.workspace = "C:\Users\Documents\Regions"
arcpy.env.overwriteOutput = True

#define projection
Prjfile = r"C:\Users\Documents\Regions\WGS1984.prj"
spRef = arcpy.SpatialReference(Prjfile)

#Local Variables
FC6 = r"C:\Users\Documents\Regions\West_DO.gdb\Candidate_Fbr_500M"
FC7 = r"C:\Users\Documents\Regions\West_DO.gdb\Search_Ring"

mxd = arcpy.mapping.MapDocument(r"C:\\Users\\Documents\\Demo_West.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

arcpy.MakeFeatureLayer_management(FC7, r"C:\Users\Documents\Regions\Search_Ring.lyr")
layer7 = arcpy.mapping.Layer(r"C:\Users\Documents\Regions\Search_Ring.lyr")
Sym_layer7 = r"C:\Users\Documents\Symbology layer new\Search Ring.lyr"
arcpy.ApplySymbologyFromLayer_management (layer7, Sym_layer7)
arcpy.mapping.AddLayer(df, layer7, "AUTO_ARRANGE")
lyr7 = arcpy.mapping.ListLayers(mxd, "", df)[0]
lyr7.visible = False
lyr7.name = "Search_Ring"
lyr7.maxScale = 0;
lyr7.minScale = 2000000;
lyr7.transparency = 25;

def FindLabel ( [Candidate_Code] ):
    return  "<CLR blue='255'><FNT size = '9'><BOL>" + [Candidate_Code] + "</BOL></FNT></CLR>"

lyr7.showLabels = True

arcpy.MakeFeatureLayer_management(FC6, r"C:\Users\Documents\Regions\Candidate_Fbr_500M.lyr")
layer6 = arcpy.mapping.Layer(r"C:\Users\Documents\Regions\Candidate_Fbr_500M.lyr")
Sym_layer6 = r"C:\Users\Documents\Symbology layer new\Candidate_Fbr_500M.lyr"
arcpy.ApplySymbologyFromLayer_management (layer6, Sym_layer6)
arcpy.mapping.AddLayer(df, layer6, "AUTO_ARRANGE")
lyr6 = arcpy.mapping.ListLayers(mxd, "", df)[0]
lyr6.visible = False
lyr6.name = "Candidate_Fbr_500M"

mxd.saveACopy(r"C:\\Users\\Documents\\West.mxd")
print "save mxd"

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd,df
2

You have a syntax error on this line

env.workspace = "C:\Users\Documents\Regions"

which is causing your script not to work. You need to either escape your slashes "C:\\Users\\Documents\\Regions" or specify as raw string r"C:\Users\Documents\Regions"

env.workspace = r"C:\Users\Documents\Regions"

When I make this change in your script my IDE reports no syntax errors.

And to be extra tidy, any lines you have as raw string you shouldn't also have double slashes (although it probably won't stop your script from functioning correctly), e.g.

mxd = arcpy.mapping.MapDocument(r"C:\\Users\\Documents\\Demo_West.mxd")

should be

mxd = arcpy.mapping.MapDocument(r"C:\Users\Documents\Demo_West.mxd")

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.