<?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: Modifying Layouts using Python Script in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195317#M57673</link>
    <description>&lt;P&gt;This also handles group layers, even nested ones:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path



def export_layout_for_each_layer(layer_list, layout, save_folder):
    save_folder = Path(save_folder)
    # turn all layers off
    for layer in layer_list:
        layer.visible = False
    # while there are still layers in the list
    while layer_list:
        # remove the first layer from the list and make it visible
        layer = layer_list.pop(0)
        layer.visible = True
        # special treatment for group layers
        if layer.isGroupLayer:
            # recursively call this function on its sub layers (this exports all the sub layers on their own
            export_layout_for_each_layer(layer.listLayers(), layout, save_folder)
            # make the sub layers visible (so we can export the group layer) and remove the sub layers from the master layer list (because we already exported them)
            for sub_layer in layer.listLayers():
                layer_list.remove(sub_layer)
                sub_layer.visible = True
        # set title and export, then make layer invisible
        print(f"Exporting layout for {layer.name}")
        layout.listElements("TEXT_ELEMENT")[0].text = layer.name
        layout.exportToPDF(save_folder/f"{layer.name}.pdf")
        layer.visible = False


# define your save location
save_folder = Path(arcpy.env.workspace).parent

# get the elements you need
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts("Layout")[0]
map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]
layers = map_frame.map.listLayers()

# run it
export_layout_for_each_layer(layers, layout, save_folder)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Jul 2022 07:33:22 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-07-25T07:33:22Z</dc:date>
    <item>
      <title>Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195287#M57669</link>
      <description>&lt;P&gt;I am currently trying to edit a preset layout on ArcGIS Pro&lt;/P&gt;&lt;P&gt;The layout currently consists of a Legend, a Title and a Map.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a Python Script, to export this layout individually for each layer to PNG.&lt;/P&gt;&lt;P&gt;For each export,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would have already snapped a particular region of the map I require so no need for any changes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Legend - it will reflect the new layer being selected&lt;/P&gt;&lt;P&gt;The Title - it will have the name of the new layer&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 03:47:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195287#M57669</guid>
      <dc:creator>sahus</dc:creator>
      <dc:date>2022-07-25T03:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195309#M57671</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path

# define your save location
save_folder = Path(arcpy.env.workspace).parent # 'C:/Users/xxx/Documents/ArcGIS/Projects/Testing'

# get the elements you need
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts("Layout")[0]
title = layout.listElements("TEXT_ELEMENT")[0]
map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]
layers = map_frame.map.listLayers()

# turn off all layers
for layer in layers:
    layer.visible = False

# loop over layers
save_folder = Path(save_folder)
for layer in layers:
    print(f"Exporting layout for {layer.name}")
    layer.visible = True
    title.text = layer.name
    layout.exportToPDF(save_folder/f"{layer.name}.pdf")
    layer.visible = False&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This assumes that you only have one text element (the title) and no group layers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 06:52:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195309#M57671</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-25T06:52:49Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195317#M57673</link>
      <description>&lt;P&gt;This also handles group layers, even nested ones:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path



def export_layout_for_each_layer(layer_list, layout, save_folder):
    save_folder = Path(save_folder)
    # turn all layers off
    for layer in layer_list:
        layer.visible = False
    # while there are still layers in the list
    while layer_list:
        # remove the first layer from the list and make it visible
        layer = layer_list.pop(0)
        layer.visible = True
        # special treatment for group layers
        if layer.isGroupLayer:
            # recursively call this function on its sub layers (this exports all the sub layers on their own
            export_layout_for_each_layer(layer.listLayers(), layout, save_folder)
            # make the sub layers visible (so we can export the group layer) and remove the sub layers from the master layer list (because we already exported them)
            for sub_layer in layer.listLayers():
                layer_list.remove(sub_layer)
                sub_layer.visible = True
        # set title and export, then make layer invisible
        print(f"Exporting layout for {layer.name}")
        layout.listElements("TEXT_ELEMENT")[0].text = layer.name
        layout.exportToPDF(save_folder/f"{layer.name}.pdf")
        layer.visible = False


# define your save location
save_folder = Path(arcpy.env.workspace).parent

# get the elements you need
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts("Layout")[0]
map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]
layers = map_frame.map.listLayers()

# run it
export_layout_for_each_layer(layers, layout, save_folder)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 07:33:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195317#M57673</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-25T07:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195334#M57676</link>
      <description>&lt;P&gt;Wow, Thanks Johannes! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is getting closer to what I would like. I also have a legend in the layout I would like to change according to the layer as well. How will I be able to do this?&lt;BR /&gt;&lt;BR /&gt;Do you have a link to this documentation for editing and exporting layouts specifically as well?&lt;BR /&gt;&lt;BR /&gt;Thank you for your assistance on this!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 09:04:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195334#M57676</guid>
      <dc:creator>sahus</dc:creator>
      <dc:date>2022-07-25T09:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195360#M57681</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I also have a legend in the layout I would like to change according to the layer as well. How will I be able to do this?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Make sure that your legend synchronizes with visible layers:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1658746688101.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/46668iAC29613E6A8887C5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_0-1658746688101.png" alt="JohannesLindner_0-1658746688101.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Do you have a link to this documentation for editing and exporting layouts specifically as well?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;You can manipulate layouts with the arcpy.mp (mapping) module:&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/2.8/arcpy/mapping/introduction-to-arcpy-mp.htm" target="_blank"&gt;Introduction to arcpy.mp—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;In the link's table of contents, expand the "Classes" tab to see the documentation for all classes of that module. Here, I used Layout, MapFrame, Map, Layer, and TextElement.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 11:05:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195360#M57681</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-25T11:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195729#M57733</link>
      <description>&lt;P&gt;Hi Johannes,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for helping me with this. I am not sure why I am unable to see such granularity in terms of properties under Legend. It stops at the General Tab and does not have the "Synchronize with map" option.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I would also like to know how I can specify which layers I would like to export, instead of all the layers on the map. I&amp;nbsp; created a list and passed it through the map_frame.map.listlayers() but it does not seem to work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have 4 group layers&amp;nbsp;["Money Stats" , "House Stats", "Car Stats", "Travel Stats"], and I only want to iterate through the layers under these group layers and export.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;&lt;P&gt;sahus&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 02:49:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195729#M57733</guid>
      <dc:creator>sahus</dc:creator>
      <dc:date>2022-07-26T02:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195747#M57736</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I am not sure why I am unable to see such granularity in terms of properties under Legend&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Hmm, no idea, but you can also do it with Python (see line 22).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I would also like to know how I can specify which layers I would like to export, instead of all the layers on the map.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;listLayers()&lt;/STRONG&gt; has an optional string argument which lets you search for layer name patterns. You could mabe rig something up with &lt;STRONG&gt;listLayers("*Stats")&lt;/STRONG&gt;, but the far easier and more expandable way is to add a second step that only keeps the layers with the names you specify. See lines 32 and 39.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path

def export_layout_for_each_layer(layer_list, layout, save_folder):
    save_folder = Path(save_folder)
    # turn all layers off
    for layer in layer_list:
        layer.visible = False
    # while there are still layers in the list
    while layer_list:
        # remove the first layer from the list and make it visible
        layer = layer_list.pop(0)
        layer.visible = True
        # special treatment for group layers
        if layer.isGroupLayer:
            # recursively call this function on its sub layers (this exports all the sub layers on their own
            export_layout_for_each_layer(layer.listLayers(), layout, save_folder)
            # make the sub layers visible (so we can export the group layer) and remove the sub layers from the master layer list (because we already exported them)
            for sub_layer in layer.listLayers():
                layer_list.remove(sub_layer)
                sub_layer.visible = True
        # make sure the legend only shows active layers
        layout.listElements("LEGEND_ELEMENT")[0].syncLayerVisibility = True
        # set title and export, then make layer invisible
        print(f"Exporting layout for {layer.name}")
        layout.listElements("TEXT_ELEMENT")[0].text = layer.name
        layout.exportToPDF(save_folder/f"{layer.name}.pdf")
        layer.visible = False


# define your save location and export layerrs
save_folder = Path(arcpy.env.workspace).parent
export_layer_names =  ["Money Stats" , "House Stats", "Car Stats", "Travel Stats"]

# get the elements you need
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts("Layout")[0]
map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]
all_layers = map_frame.map.listLayers()
export_layers = [layer for layer in all_layers if layer.name in export_layer_names]

# run it
export_layout_for_each_layer(export_layers, layout, save_folder)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 06:20:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1195747#M57736</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-26T06:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1196210#M57820</link>
      <description>&lt;P&gt;Hi Johannes,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help! The code makes sense, but it throws me a ValueError. I think the ".remove" function causes the issue.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sahus_1-1658897416575.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/46873iEE64360BB5FA2A62/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sahus_1-1658897416575.png" alt="sahus_1-1658897416575.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried using pop as well.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sahus_0-1658897303713.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/46872iCB62073A489BBECE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sahus_0-1658897303713.png" alt="sahus_0-1658897303713.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Do let me know if you have workarounds for this! Really appreciate your help thus far!! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;/P&gt;&lt;P&gt;sahus&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 04:51:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1196210#M57820</guid>
      <dc:creator>sahus</dc:creator>
      <dc:date>2022-07-27T04:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1196216#M57822</link>
      <description>&lt;P&gt;Yeah, I didn't test before posting...&lt;/P&gt;&lt;P&gt;It tries to remove sublayers from the list, but they aren't in the list because you removed them in line 39 above.&lt;/P&gt;&lt;P&gt;Just let it fail silently:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;                try:
                    layer_list.remove(sub_layer)
                except ValueError:
                    pass  # sub_layer not in layer_list&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 05:30:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1196216#M57822</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-07-27T05:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Modifying Layouts using Python Script</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1197210#M57929</link>
      <description>&lt;P&gt;Hi Johannes,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;It worked great, thank you for your help! Really appreciate the clarifications and direction!&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Sahus&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 01:36:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/modifying-layouts-using-python-script/m-p/1197210#M57929</guid>
      <dc:creator>sahus</dc:creator>
      <dc:date>2022-07-29T01:36:15Z</dc:date>
    </item>
  </channel>
</rss>

