1

I am using arcpy to pull in layers from a file geodatabase but I receive the following error below. How can I assign symbology from a .lyr file to a TOC layer using arcpy?

# Name: Create Map
# Purpose: Fixes bugs and other mapping problems 

# Import system modules
import arcpy
from arcpy import env

# Set the current workspace
env.workspace = "I:\UPDM.gdb"

# Rebuild Map Workspace
#
mxd = arcpy.mapping.MapDocument(r"C:\Users\aa2zz6\Desktop\test\Untitled.mxd")

df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
# Add layer to TOC
#
addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_Meters")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

#Set layer to apply symbology to
inputLayers = ["P_Meters",]

# Set layer that output symbology will be based on
symbologyLayer = (r"C:\Temp\Meters.lyr")

# Apply the symbology from the symbology layer to the input layer
for layer in inputLayers:
arcpy.ApplySymbologyFromLayer_management (layer, symbologyLayer)

Error

Traceback (most recent call last):
File "C:\Users\aa2zz6\Downloads\CREATE NEW MXD.py", line 29, in <module>
arcpy.ApplySymbologyFromLayer_management (layer, symbologyLayer)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", 
line 6934, in ApplySymbologyFromLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Layer: Dataset P_Meters does not exist or is not 
supported
Failed to execute (ApplySymbologyFromLayer).
3
  • Your addLayer = ... line is referencing a Feature Class, not a Layer. See desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…
    – Midavalo
    Commented Jan 27, 2018 at 3:29
  • I fixed the first section where it imports the layers from the geodatabase but I can't for the life of me figure out how to apply symbology even after looking at examples on here and Esri website. The inputlayer is looking for the layer inside the TOC while the symbologylayer is looking at the .lyr file location. Am I reading this incorrectly in how it's setup with ApplySymbologyFromLayer_management(layer, symbologyLayer)?
    – aa2zz6
    Commented Jan 27, 2018 at 4:03
  • I was doing some testing and I notice that the ApplySymbologyFromLayer works inside ArcMap python command window but it doesn't work outside of the map in python IDLE. Is there a reason why it would work in 1 instance verses the other?
    – aa2zz6
    Commented Jan 27, 2018 at 13:47

1 Answer 1

1

Since this post was cross-posted I wanted to share the solution that solved this issue.

Credit: Randy Burton

Original Source: https://community.esri.com/thread/208705-apply-symbology-to-toc-layers

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Path\To\SymbolTest.mxd")
# layer file is in directory with mxd map document; this will be the 
workspace
arcpy.env.workspace = os.path.dirname(mxd.filePath)

# dictonary matches map layer with symbology layer file
# layer name in TOC : name of layer file (can use full path/file name if not in map directory)
symbols = {'SymbolTest':'LayerSymbology.lyr'}

for layer in arcpy.mapping.ListLayers(mxd):
    if layer.name in symbols:
    print "Layer: '{}' - previous symbology type: '{}'.".format(layer.name, layer.symbologyType)
    # http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/apply-symbology-from-layer.htm
    # ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)
    print "Applying symbology to layer '{}' using '{}'.".format(layer.name, symbols[layer.name])
    arcpy.ApplySymbologyFromLayer_management(layer, symbols[layer.name])
    print "Layer: '{}' - new symbology type: '{}'.".format(layer.name, layer.symbologyType)
else:
print "Layer '{}' not updated.".format(layer.name)

# may need to refresh map
# arcpy.RefreshTOC()
# arcpy.RefreshActiveView()

del mxd

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.