I have a .lyr file on my desktop that I would like apply symbology to a TOC sde layer in an existing map document. How can I do this using python IDLE?
Looks like you are on the right track. You can use ApplySymbology outside with IDLE; what type of error do you get when using IDLE.
Here's a script that I use to apply symbology using a layer file:
<SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="comment token"># if outside ArcMap</SPAN> <SPAN class="comment token"># inside ArcMap, use "CURRENT" for document name</SPAN> mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\Path\To\SymbolTest.mxd"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># layer file is in directory with mxd map document; this will be the workspace</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">.</SPAN>filePath<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># dictonary matches map layer with symbology layer file</SPAN> <SPAN class="comment token"># layer name in TOC : name of layer file (can use full path/file name if not in map directory)</SPAN> symbols <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'SymbolTest'</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">'LayerSymbology.lyr'</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">for</SPAN> layer <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> layer<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">in</SPAN> symbols<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Layer: '{}' - previous symbology type: '{}'."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN> layer<SPAN class="punctuation token">.</SPAN>symbologyType<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"><SPAN># </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Farcmap%2Flatest%2Ftools%2Fdata-management-toolbox%2Fapply-symbology-from-layer.htm" target="_blank">http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/apply-symbology-from-layer.htm</A></SPAN> <SPAN class="comment token"># ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Applying symbology to layer '{}' using '{}'."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN> symbols<SPAN class="punctuation token">[</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ApplySymbologyFromLayer_management<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> symbols<SPAN class="punctuation token">[</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Layer: '{}' - new symbology type: '{}'."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN> layer<SPAN class="punctuation token">.</SPAN>symbologyType<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Layer '{}' not updated."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># may need to refresh map</SPAN> <SPAN class="comment token"># arcpy.RefreshTOC()</SPAN> <SPAN class="comment token"># arcpy.RefreshActiveView()</SPAN> <SPAN class="keyword token">del</SPAN> mxd<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hi Jordan,
In following line of script "symbolLyr" is defined as "ymbolLyr", as stated in error trace back.
# Set layer that output symbology will be based on
ymbolLyr = r"C:\Users\aa2zz6\Desktop\test\Meters.lyr"
Hi Dan,
I did try using the example code but it didn't work. I'll post my code and error.
Edit: If I run the symbology to TOC part of the script inside the map python it works great. Why is it not able to run outside in IDLE?
# Name: Create Map
# Import system modulesimport arcpyfrom arcpy import env
# 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")
addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_MeterSetting")arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_Regulator")arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
addLayer = arcpy.mapping.Layer(r"I:\UPDM.gdb\P_PipeSystem\P_TownBorderStation")arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.save()
# Apply Symbology to TOC layer
#Set layer to apply symbology tolyr = "P_Meters"
# Set layer that output symbology will be based onymbolLyr = r"C:\Users\aa2zz6\Desktop\test\Meters.lyr"
# Apply the symbology from the symbology layer to the input layerarcpy.ApplySymbologyFromLayer_management(lyr, symbolLyr)
Error
Traceback (most recent call last): File "C:\Users\aa2zz6\Downloads\CREATE NEW MXD.py", line 38, in arcpy.ApplySymbologyFromLayer_management(lyr, symbolLyr) NameError: name 'symbolLyr' is not defined
Did you see Apply Symbology from Layer there is a code sample at the end.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.