import arcpy
import arcpy.mapping
import os
import sys
from arcpy import env
#import string
env.workspace = r"G:\GIS"
mxds = arcpy.ListFiles("*.mxd")
lyrs = arcpy.ListFiles("*.lyr")
for maps in mxds:
df = arcpy.mapping.ListDataFrames(mxds, "Layers")[0]
arcpy.mapping.AddLayer(df ,lyrs ,"AUTO_ARRANGE")
maps.save()
del mxds, maps, df, lyrs
1 Answer
Your code is not going to work for these reasons:
- You need to add a wildcard "*" to ".mxd" and ".lyr" so that these return something (lines 3 and 4)
- You referenced "mxds" instead of "maps" on line 6, and it needs to be referenced as a arcpy.mapping.MapDocument because you need to save later.
- You need to nest the add each layer in a loop (line 8)
- You need to save the changes to the MapDocument object, not the mxd name (line 11)
Credit to this answer on adding layers.
Try this fix:
import arcpy
arcpy.env.workspace = r"G:\GIS"
mxds = arcpy.ListFiles("*.mxd")
lyrs = arcpy.ListFiles("*.lyr")
for maps in mxds:
mxd = arcpy.mapping.MapDocument(maps)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for layer in lyrs:
addlayer = arcpy.mapping.Layer(layer)
arcpy.mapping.AddLayer(df ,addlayer ,"AUTO_ARRANGE")
mxd.save()