Reformat all layer names in an ArcMap document

*Before I get into today’s script, I’ve noticed that the embedded gists do not appear on mobile devices when accessing the feed for this blog via Feedly or some other RSS reader. The post needs to be opened in a web browser to view the code. I’ll also be including a link to it at the end of the post.

When I create feature classes for asset management data manually instead of automatically, I always make sure to name them consistently. However, I rarely change the feature class alias manually. This means that when I need to pull all the layers into a map to create a kml, I need to rename each layer.

Which is why I turn to Python instead.

# @date 14/08/2015
# @author Cindy Williams
# Quickly formats the names of all the layers in a map
# document. In this example, the part of the name before
# the first full stop is removed, underscores are replaced
# spaces, a regex is used to insert spaces before capitals
# and the string is converted to proper case.
# For use in the Python window in ArcMap.
import arcpy
import re
mxd = arcpy.mapping.MapDocument("CURRENT")
lyrs = arcpy.mapping.ListLayers(mxd)
for lyr in lyrs:
new_name_replaced =".")[1].replace("_", " ")
new_name_spaced = re.sub(r"(\w)([A-Z])", r"\1 \2", new_name_replaced) = new_name_spaced.title()

Of course, the way the name gets split will change depending on the current format of the names. In this instance, the part of the name that I needed occurred after the first full stop, and contained underscores which needed to be replaced by spaces. I then used a regular expression to insert spaces between InitialCaps words.

Link to the code is here.

One thought on “Reformat all layer names in an ArcMap document

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.