import arcpy add_layer = arcpy.mapping.Layer('C:\\Path\\to\\lyrfile.lyr') mxds_path = 'c:\\path\\to\\mxds\\' mxds = ['one.mxd', 'two.mxd'] for mxd_name in mxds: mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name) df = arcpy.mapping.ListDataFrames(mxd, 'Layers1')[0] arcpy.mapping.AddLayer(df, addLayer, "TOP") mxd.save()
Good thread. Is their an easy way to do this where you want to do it to all MXDs in a folder?
# Import the arcpy module import arcpy # Identify the path to the Layer File you want to insert on disk. InsertLayer = arcpy.mapping.layer(r"C:\MyGIS\Layers\TheLayerFileToInsert.lyr") # Identify the Folder containing the MXDs you want to insert the 'InsertLayer' into MXDsFolder = r"C:\MyGIS\MXDsFolder" # Set the Environment Workspace to the MXDs Folder so that the 'ListFiles()' # function will search this folder for MXDs when called arcpy.env.workspace = MXDsFolder # Call the 'arcpy.ListFiles()' function and invoke the wildcard parameter to only # return MXDs from the folder. Populate the results in the variable 'MXDsList' MXDsList = arcpy.ListFiles("*.mxd") # Loop through each MXD in the 'MXDsList' for MXD in MXDsList: # Establish the current MXD as the 'TargetMXD' to insert the Layer into TargetMXD = arcpy.mapping.MapDocument(MXDsFolder + "\\" + str(MXD)) # Get a reference to the first dataframe in the Target MXD TargetDF = arcpy.mapping.ListDataFrames(TargetMXD)[0] # Add the Layer to the TargetMXD arcpy.mapping.AddLayer(TargetDF, InsertLayer, "TOP") # Save the MXD TargetMXD.save() print "Complete"
import arcpy, os folderPath = r"C:\\01Working_Directory\\LandscapeMaps" # Set the Environment Workspace to the MXDs Folder so that the 'ListFiles()' # function will search this folder for MXDs when called arcpy.env.workspace = folderPath # Call the 'arcpy.ListFiles()' function and invoke the wildcard parameter to only # return MXDs from the folder. Populate the results in the variable 'MXDsList' MXDsList = arcpy.ListFiles("*.mxd") # Loop through each MXD in the 'MXDsList' for MXD in MXDsList: # Establish the current MXD as the 'TargetMXD' to insert the Layer into TargetMXD = arcpy.mapping.MapDocument(folderPath + "\\" + str(MXD)) for filename in os.listdir(folderPath): fullpath = os.path.join(folderPath, filename) if os.path.isfile(fullpath): basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) mxd.findAndReplaceWorkspacePaths(r"c:\01Working_Directory\LandscapeMaps\LandscapeDistrict.gdb", r"G:\CamarilloGIS\Projects\PublicWorks\STREETS\Landscape Districts\LandscapeDistricts.gdb") TargetMXD.save() print "All Repathed!"
>>> folderPath = r"C:\\01Working_Directory\\LandscapeMaps" >>> file = "MyGISFile" >>> os.path.join(folderPath, file) 'C:\\\\01Working_Directory\\\\LandscapeMaps\\MyGISFile' >>>
Hi! Is there a way to insert the feature layers from a gdb with a specific formatting, using as a base the code that TomMagdaleno used? I'm in ArcMap 10.8. Maybe there is something I can fix in the feature layers in the gdb so they always are added with the same specific format. Thanks in advance!