rename layers displayed on TOC

13512
13
Jump to solution
01-10-2012 05:27 AM
FugroSurvey_Ltd_
New Contributor II
I???m brand new to Python so I???m not sure if what I want to do is possible or not and I apologise for my lack of technical terms.

Basically I need to export a few files from ArcMap to CAD on a daily basis. As part of company procedures my CAD layers need to have a specific name which is different from the original GIS layer name. The problem is that the Export to CAD tool picks up the name displayed on the TOC to create levels' name in CAD.
Having said that what I'm looking for is to make a script to rename all the layers on my TOC. Is this possible?

I appreciate any help or pointers in the right direction.

Thanks

PT
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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()

View solution in original post

13 Replies
MathewCoyle
Frequent Contributor
I think you want to access the arcpy.mapping.Layer(layer).name property.
See here http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000008000000

Here's a more complete example.
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()
Yaron_YosefCohen
Occasional Contributor II

how do it for 4 mxd's in one folder?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

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()

Yaron_YosefCohen
Occasional Contributor II

thank's- it's worked!! how do i mark your answer as the right one?

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

I changed your "discussion" to a "question" and marked Jake's answer as correct for you. 

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

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 ?

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

also i have this question Insert layer file to specific data frame

can you see it please?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Branch multiple questions into individual ones and close the this thread by marking "helpful" or "like" to those that helped

0 Kudos
Raphael_Augusto_FoscariniFerre
Occasional Contributor

I put mxd.save() at the end of the script, to ensure keep the changing.

0 Kudos