2

I found this, under the topic How to iterate through layers of an MXD? answered by Midavalo

I wanto to know if its possible to change something in the second line, to iterate through all data frames in the mxd. It has the option to choose wich df, changing the number inside the racket.

I'm asking that because i want to run a script in all my mxd, in a folder. But i have a lot of them, and some have 3, 4 or five df.

Example: The script gets error, if i choose to run it in [5] and the mxd doesnt have it. So the script stops.

mxd = arcpy.mapping.MapDocument("CURRENT")  # Uses your currently open MXD
df = arcpy.mapping.ListDataFrames(mxd, '')[0] # Chooses the first dataframe
for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
    # Any tools you want to run on each layer go here
2

It would be the same as iterating through the layers.

mxd = arcpy.mapping.MapDocument("CURRENT")  # Uses your currently open MXD
for df in arcpy.mapping.ListDataFrames(mxd, ''): # Loop through dataframes
    for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
        # Any tools you want to run on each layer go here
  • Yeah, thanks!!I I just put put a : in the end of your second line!! – raphael Jun 15 '16 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.