Hi Y.Y.C,
You can do this using the ListFiles function. Ex:
import arcpy from arcpy import env env.workspace = r"C:\temp\python" for mxdFile in arcpy.ListFiles("*.mxd"): mxdPath = env.workspace + "\\" + mxdFile mxd = arcpy.mapping.MapDocument(mxdPath) layers = arcpy.mapping.ListLayers(mxd) for lyr in layers: if lyr.name == "something to change": lyr.name = "changed name" arcpy.RefreshTOC()
I figured how to do it. I just need to change it, the way i want one time, than save a .lyr from the correct information (it can also change symbology, and other stuff).
The script below, will apply the .lyr file to your layer in the TOC. You just must specify the name of the layer, like displayed in TOC.
It will do to all MXD files, in the same folder of the script.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os <SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>curdir <SPAN class="keyword token">for</SPAN> mxdFile <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*.mxd"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> mxdPath <SPAN class="operator token">=</SPAN> env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> mxdFile mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>mxdPath<SPAN class="punctuation token">)</SPAN> layers <SPAN class="operator token">=</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="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> layers<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"layer_name_in_TOC"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#change here</SPAN> <SPAN class="keyword token">print</SPAN> mxdPath symbologyLayer <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"D:\LayerFile.lyr"</SPAN> <SPAN class="comment token">#change here</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ApplySymbologyFromLayer_management <SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> symbologyLayer<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RefreshTOC<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><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>
i'm just trying to get as many options as possible.
since you posted this question https://community.esri.com/message/673130-re-how-can-i-change-the-layer-label-in-toc-with-py you may want to delete your comment/question above. If geonet doesn't allow you to, I might be able to do it for you.
Is there a way to change the 'label' of the layerfile? The script above, changes the layer name. But i want to change the layer name. I'm refering to the name is written right after the symbol, in the TOC.
also i have this question Insert layer file to specific data frame
can you see it please?
now,i have mxd's and each of them has 3 and more data frame. if I wan't to rename layer in a specific data frame with position in the TOC, for example in the first data frame, how to do it ?
thank's- it's worked!! how do i mark your answer as the right one?
how do it for 4 mxd's in one folder?
mxd = arcpy.mapping.MapDocument("current") layers = arcpy.mapping.ListLayers(mxd) for lyr in layers: if lyr.name == "something to change": lyr.name = "changed name" arcpy.RefreshTOC()
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.