Skip to content

Instantly share code, notes, and snippets.

@cindygis
Last active August 29, 2015 14:27
Show Gist options
  • Save cindygis/99da1dbf5603a9fb5469 to your computer and use it in GitHub Desktop.
Save cindygis/99da1dbf5603a9fb5469 to your computer and use it in GitHub Desktop.
Quickly formats the names of all the layers in a map document.
#
# @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 = lyr.name.split(".")[1].replace("_", " ")
new_name_spaced = re.sub(r"(\w)([A-Z])", r"\1 \2", new_name_replaced)
lyr.name = new_name_spaced.title()
arcpy.RefreshTOC()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment