<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Adding several .lyr into different MXD stored in one folder with several sub-folder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193839#M65022</link>
    <description>&lt;P&gt;Sorted&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jul 2022 15:37:21 GMT</pubDate>
    <dc:creator>Paul-EmmanuelLeroux</dc:creator>
    <dc:date>2022-07-20T15:37:21Z</dc:date>
    <item>
      <title>Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193281#M65008</link>
      <description>&lt;P&gt;Hello community,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;I have another question where is this specific script supposed to be run, in Python for Arcmap or Python console on Windows?&lt;/P&gt;&lt;PRE&gt;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()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Paulo&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 07:05:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193281#M65008</guid>
      <dc:creator>Paul-EmmanuelLeroux</dc:creator>
      <dc:date>2022-07-19T07:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193306#M65009</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/603735"&gt;@Paul-EmmanuelLeroux&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I originally wrote this in StackExchange &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;How have you tried to run it so far? What errors are you getting? Details and screenshots will help.&lt;/P&gt;&lt;P&gt;As a personal preference I use PyScripter to run Python Scripts outside of the ArcMap/Pro environment.&lt;/P&gt;&lt;P&gt;You can also use IDLE that is installed with Python, or you can create/load a script within the ArcMap/Pro software.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 09:45:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193306#M65009</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2022-07-19T09:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193828#M65020</link>
      <description>&lt;P&gt;In IDLE the error appear after the last part:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;gt;&amp;gt;&amp;gt; for lyr_file in add_lyrs:&lt;BR /&gt;add_lyr = m.Layer(lyr_file)&lt;BR /&gt;m.AddLayer(df, add_lyr ,"TOP")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;pyshell#11&amp;gt;", line 2, in &amp;lt;module&amp;gt;&lt;BR /&gt;add_lyr = m.Layer(lyr_file)&lt;BR /&gt;File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\arcobjects\mixins.py", line 429, in __init__&lt;BR /&gt;super(LayerMixin, self).__init__(lyrfile)&lt;BR /&gt;File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\arcobjects\_base.py", line 47, in __init__&lt;BR /&gt;for arg in args))&lt;BR /&gt;ValueError: Objet\xa0: source of data not valid in the layer CreateObject&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 08:49:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193828#M65020</guid>
      <dc:creator>Paul-EmmanuelLeroux</dc:creator>
      <dc:date>2022-07-20T08:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193829#M65021</link>
      <description>&lt;P&gt;Can you show the full code you are running? It could be a path error to the lyr files you are attempting to use.&lt;/P&gt;&lt;P&gt;What have you added for the paths here?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;add_lyr_1 = r"path\to\file1.lyr"
add_lyr_2 = r"path\to\file2.lyr"
add_lyr_3 = r"path\to\file3.lyr"&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 20 Jul 2022 08:53:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193829#M65021</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2022-07-20T08:53:10Z</dc:date>
    </item>
    <item>
      <title>Re: Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193839#M65022</link>
      <description>&lt;P&gt;Sorted&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 15:37:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193839#M65022</guid>
      <dc:creator>Paul-EmmanuelLeroux</dc:creator>
      <dc:date>2022-07-20T15:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding several .lyr into different MXD stored in one folder with several sub-folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193900#M65024</link>
      <description>&lt;P&gt;I corrected something in the first loop:&lt;/P&gt;&lt;P&gt;for root, dirs, files in os.walk(root_folder):&lt;BR /&gt;for f in files:&lt;BR /&gt;if f[-4:]==(".mxd"):&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;in the second loop I have a problem with&amp;nbsp;&lt;SPAN&gt;mxd = m.MapDocument(mxd_doc)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 15:37:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-several-lyr-into-different-mxd-stored-in/m-p/1193900#M65024</guid>
      <dc:creator>Paul-EmmanuelLeroux</dc:creator>
      <dc:date>2022-07-20T15:37:54Z</dc:date>
    </item>
  </channel>
</rss>

