Hello community,
I am trying to add the 3 same .lyr layers into different mxd in a folder with serveral sub-folders and many sub-sub folders.
I was helped to draw a script. However, I failed to run it. I would really apprciate if the community could help me to correct the script PLEASE!
I have another question where is this specific script supposed to be run, in Python for Arcmap or Python console on Windows?
import arcpy
import arcpy.mapping as m
import os
root_folder = r"path\to\root\folder"
add_lyr_1 = r"path\to\file1.lyr"
add_lyr_2 = r"path\to\file2.lyr"
add_lyr_3 = r"path\to\file3.lyr"
## create list of lyr files
add_lyrs = [add_lyr_1, add_lyr_2, add_lyr_3]
mxd_paths = []
## add all mxd paths to a list
for root, dirs, files in os.walk (root_folder):
for f in files:
if f.endswith(".mxd"):
mxd_paths.append("{0}\\{1}".format(root, f))
## print to test
#print(mxd_paths)
for mxd_doc in mxd_paths:
mxd_name = mxd_doc.rsplit("\\")[-1]
arcpy.AddMessage(mxd_name)
## access the mxd
mxd = m.MapDocument(mxd_doc)
## only access the desired dataframe
## you might want to use a dataframe name / wildcard to
## add layers to correct dataframe if you have multiple
df = m.ListDataFrames(mxd)[0]
## add each layer file, one on top of the other.
for lyr_file in add_lyrs:
add_lyr = m.Layer(lyr_file)
m.AddLayer(df, add_lyr ,"TOP")
mxd.save()
Paulo