<?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: Trying to export a series of layouts with different definition queries and extent with arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1261137#M66925</link>
    <description>&lt;P&gt;Thank you! This led me to the solution, and although I don't need the text box now, thank you for showing me how to do it!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I ended up doing.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;#Project path&lt;/SPAN&gt;
aprx = arcpy.mp.ArcGISProject(
    &lt;SPAN class=""&gt;r"projectpath.aprx"&lt;/SPAN&gt;)


&lt;SPAN class=""&gt;# Set the name of the field that contains the class names&lt;/SPAN&gt;
class_field = &lt;SPAN class=""&gt;"Species"&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# Set Map View and Layout and Map Frame&lt;/SPAN&gt;
map_view = aprx.listMaps(&lt;SPAN class=""&gt;"Map"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
layout = aprx.listLayouts(&lt;SPAN class=""&gt;"Layout2"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
map_frame = layout.listElements(&lt;SPAN class=""&gt;"MAPFRAME_ELEMENT"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set species layer&lt;/SPAN&gt;
species_lyr = map_view.listLayers(&lt;SPAN class=""&gt;"ScioSpecies"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set shapefile polygon to custom extent&lt;/SPAN&gt;
customextent = map_view.listLayers(&lt;SPAN class=""&gt;"customextent"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set output&lt;/SPAN&gt;
output_folder = output path

&lt;SPAN class=""&gt;#clear out all the existing definitionqueries&lt;/SPAN&gt;
species_lyr.updateDefinitionQueries([])


&lt;SPAN class=""&gt;# Grab each species, and make sure all are present&lt;/SPAN&gt;
class_list = []
&lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.SearchCursor(species_lyr, class_field) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; cursor:
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; row &lt;SPAN class=""&gt;in&lt;/SPAN&gt; cursor:
        &lt;SPAN class=""&gt;if&lt;/SPAN&gt; row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;] &lt;SPAN class=""&gt;not&lt;/SPAN&gt; &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
            class_list.append(row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;])

&lt;SPAN class=""&gt;print&lt;/SPAN&gt;(class_list)


&lt;SPAN class=""&gt;#set the extent based on the species layer &lt;/SPAN&gt;
map_frame.camera.setExtent(map_frame.getLayerExtent(customextent, &lt;SPAN class=""&gt;False&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;#Loop for each species&lt;/SPAN&gt;
&lt;SPAN class=""&gt;for&lt;/SPAN&gt; species &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
    species_lyr.definitionQuery = &lt;SPAN class=""&gt;"{0} = '{1}'"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(class_field,species)
    outfile = &lt;SPAN class=""&gt;"{0}\\{1}.pdf"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(output_folder,species)
    layout.exportToJPEG(
        exportfolde&lt;SPAN class=""&gt;r".format(species),
        resolution=900)
    print("&lt;/SPAN&gt;Successful export of &lt;SPAN class=""&gt;", species)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I'm sure it's not the prettiest code, but it got the output I wanted, having the custom rectangle to define extent really helped to standardize all of them.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Feb 2023 21:59:47 GMT</pubDate>
    <dc:creator>TeaganMulford</dc:creator>
    <dc:date>2023-02-23T21:59:47Z</dc:date>
    <item>
      <title>Trying to export a series of layouts with different definition queries and extent with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1259945#M66905</link>
      <description>&lt;P&gt;I'm on ArcGIS Pro (self-taught) and I am trying to make a batch of layouts for different species occurrence records over the same extent. I have all the XY to Point data as a layer and have been using the display filter to limit which species is shown, then manually exporting the layout, and changing the species again. All other factors are the same, save the species. I want to automate this since I have 80 or so to do (and do this often). Using Map series hasn't worked, since the extent always changes, so I thought I would attempt with arcpy since I know a little bit about python...&lt;/P&gt;&lt;P&gt;Ideally, I want to create a layout for each species, with the same extent, layers, etc, but with a different species selected from the XY to Point layer. I then want to export the layout with the name of the species selected.&lt;/P&gt;&lt;P&gt;This is the code I have so far, and it all seems to work (i.e. it exports a pdf for each, with the layers I can see looking good, and runs without errors) but it has an odd extent, off to the right of the continental U.S., and a North arrow that is not in the layout I am calling. I got the extent values from the extent on the formatted layout/mapframe by looking in properties and copying the values there. Is there a way to edit the extent of the layout?&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; arcpy, os

&lt;SPAN class=""&gt;#Project path&lt;/SPAN&gt;
aprx = arcpy.mp.ArcGISProject(
    &lt;SPAN class=""&gt;r"pathtoproject.aprx"&lt;/SPAN&gt;)

&lt;SPAN class=""&gt;# Set the name of the XY to Point table&lt;/SPAN&gt;
table_name = &lt;SPAN class=""&gt;"ScioSpecies"&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# Set the name of the field that contains the class names&lt;/SPAN&gt;
class_field = &lt;SPAN class=""&gt;"Species"&lt;/SPAN&gt;

&lt;SPAN class=""&gt;#Set a reference to the map view in my project&lt;/SPAN&gt;
map_view = aprx.listMaps()[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]


&lt;SPAN class=""&gt;# Get a reference to the layout&lt;/SPAN&gt;
layout = aprx.listLayouts()[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;#Setting up layout/mapframe&lt;/SPAN&gt;
map_frame = layout.listElements(&lt;SPAN class=""&gt;"MAPFRAME_ELEMENT"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
extent = arcpy.Extent(&lt;SPAN class=""&gt;5331425.860609497&lt;/SPAN&gt;, -&lt;SPAN class=""&gt;12839586.820429197&lt;/SPAN&gt;, -&lt;SPAN class=""&gt;12017230.660916878&lt;/SPAN&gt;, &lt;SPAN class=""&gt;4267200.242417085&lt;/SPAN&gt;)
map_frame.camera.setExtent(extent)

class_list = []
&lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.SearchCursor(table_name, class_field) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; cursor:
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; row &lt;SPAN class=""&gt;in&lt;/SPAN&gt; cursor:
        &lt;SPAN class=""&gt;if&lt;/SPAN&gt; row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;] &lt;SPAN class=""&gt;not&lt;/SPAN&gt; &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
            class_list.append(row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;])
&lt;SPAN class=""&gt;#Verify all species are present&lt;/SPAN&gt;
&lt;SPAN class=""&gt;print&lt;/SPAN&gt;(class_list)

&lt;SPAN class=""&gt;for&lt;/SPAN&gt; species &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
    arcpy.SelectLayerByAttribute_management(table_name, &lt;SPAN class=""&gt;"NEW_SELECTION"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"Species = '{}'"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(species))
    layout.exportToJPEG(
        &lt;SPAN class=""&gt;r"pathtosavefile{}.jpg"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(species),
        resolution=&lt;SPAN class=""&gt;800&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2023 23:30:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1259945#M66905</guid>
      <dc:creator>TeaganMulford</dc:creator>
      <dc:date>2023-02-20T23:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to export a series of layouts with different definition queries and extent with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1260034#M66906</link>
      <description>&lt;P&gt;Since your headline asks about definition queries I'll add to your code for that. If you aren't getting the layout you expect, you might want to use the wildcard feature in the listLayouts function to find the name of the layout. If the extent doesn't change on the layout between exports, then you probably don't need to change it. Or, you can get the layer's extent and set the camera to that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;map_view = aprx.listMaps("SpeciesMapName")[0]
layout = aprx.listLayouts("SpeciesLayoutName")[0]
map_frame = layout.listElements("MAPFRAME_ELEMENT")[0]
species_lyr = map_view.listLayers("ScioSpecies")[0]
#you can have a text box in your layout with a name, and change it to #species name
species_txt = None
for txt in layout.listElements("TEXT_ELEMENT"):
    if txt.name == "SpeciesName":
        species_txt = txt
        break
class_list = [row[0] for row in arcpy.da.SearchCursor(species_lyr,class_field)]
class_list = set(class_list)
#clear out all the existing definitionqueries
species_lyr.updateDefinitionQueries([])
for species in class_list:
    if species_txt is not None:
        species_txt.text = species
    species_lyr.definitionQuery = "{0} = '{1}'".format(class_field,species)
    #set the extent based on the species layer 
    map_frame.camera.setExtent(map_frame.getLayerExtent(species_lyr, False, True))
    #map_frame.camera.scale = scale_value
    outfile = "{0}\\{1}.pdf".format(output_folder,species)
    layout.exportToPDF(outfile, 300, 'BEST',output_as_image=True)
    time.sleep(1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 13:58:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1260034#M66906</guid>
      <dc:creator>dslamb2022</dc:creator>
      <dc:date>2023-02-21T13:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to export a series of layouts with different definition queries and extent with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1261137#M66925</link>
      <description>&lt;P&gt;Thank you! This led me to the solution, and although I don't need the text box now, thank you for showing me how to do it!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I ended up doing.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;#Project path&lt;/SPAN&gt;
aprx = arcpy.mp.ArcGISProject(
    &lt;SPAN class=""&gt;r"projectpath.aprx"&lt;/SPAN&gt;)


&lt;SPAN class=""&gt;# Set the name of the field that contains the class names&lt;/SPAN&gt;
class_field = &lt;SPAN class=""&gt;"Species"&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# Set Map View and Layout and Map Frame&lt;/SPAN&gt;
map_view = aprx.listMaps(&lt;SPAN class=""&gt;"Map"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
layout = aprx.listLayouts(&lt;SPAN class=""&gt;"Layout2"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
map_frame = layout.listElements(&lt;SPAN class=""&gt;"MAPFRAME_ELEMENT"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set species layer&lt;/SPAN&gt;
species_lyr = map_view.listLayers(&lt;SPAN class=""&gt;"ScioSpecies"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set shapefile polygon to custom extent&lt;/SPAN&gt;
customextent = map_view.listLayers(&lt;SPAN class=""&gt;"customextent"&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]

&lt;SPAN class=""&gt;# Set output&lt;/SPAN&gt;
output_folder = output path

&lt;SPAN class=""&gt;#clear out all the existing definitionqueries&lt;/SPAN&gt;
species_lyr.updateDefinitionQueries([])


&lt;SPAN class=""&gt;# Grab each species, and make sure all are present&lt;/SPAN&gt;
class_list = []
&lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.SearchCursor(species_lyr, class_field) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; cursor:
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; row &lt;SPAN class=""&gt;in&lt;/SPAN&gt; cursor:
        &lt;SPAN class=""&gt;if&lt;/SPAN&gt; row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;] &lt;SPAN class=""&gt;not&lt;/SPAN&gt; &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
            class_list.append(row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;])

&lt;SPAN class=""&gt;print&lt;/SPAN&gt;(class_list)


&lt;SPAN class=""&gt;#set the extent based on the species layer &lt;/SPAN&gt;
map_frame.camera.setExtent(map_frame.getLayerExtent(customextent, &lt;SPAN class=""&gt;False&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;#Loop for each species&lt;/SPAN&gt;
&lt;SPAN class=""&gt;for&lt;/SPAN&gt; species &lt;SPAN class=""&gt;in&lt;/SPAN&gt; class_list:
    species_lyr.definitionQuery = &lt;SPAN class=""&gt;"{0} = '{1}'"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(class_field,species)
    outfile = &lt;SPAN class=""&gt;"{0}\\{1}.pdf"&lt;/SPAN&gt;.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(output_folder,species)
    layout.exportToJPEG(
        exportfolde&lt;SPAN class=""&gt;r".format(species),
        resolution=900)
    print("&lt;/SPAN&gt;Successful export of &lt;SPAN class=""&gt;", species)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I'm sure it's not the prettiest code, but it got the output I wanted, having the custom rectangle to define extent really helped to standardize all of them.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 21:59:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-export-a-series-of-layouts-with/m-p/1261137#M66925</guid>
      <dc:creator>TeaganMulford</dc:creator>
      <dc:date>2023-02-23T21:59:47Z</dc:date>
    </item>
  </channel>
</rss>

