Python Script to List Visible Layers in Multiple MXDs?

06-05-2020 12:10 PM
New Contributor

Afternoon all,

I have about 50 mxd's I need to go through and export all active layers in the maps to a new geodatabase. 

I've reviewed some of the online arcpy functions and have tested the ListLayers script. However, does anyone have a good script to recurse through a directory of MXD documents, find only those layers that are currently checked on in each map, and export them to a new geodatabase?

I'm also a bit of a novice to python scripting. Any help would be greatly appreciated!

0 Kudos
1 Reply
Esri Esteemed Contributor

Hi Kacey,

I haven't tested, but you could use something similar to below:

import arcpy, os

# Directory that contains MXDs
arcpy.env.workspace = r"C:\Projects\MXDs"

# Get list of MXDs
mxds = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs
for mxd in mxds:
    print("Processing {0}".format(mxd))
    # Create MXD object
    mxd = arcpy.mapping.MapDocument(os.path.join(arcpy.env.workspace, mxd))
    # Get Data Frames
    dfs = arcpy.mapping.ListDataFrames(mxd)
    # Iterate through each layer in each dataframe
    for df in dfs:
        for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
            # If layer is visible print layer name
            if lyr.visible:
               print("\t" +