<?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: export map layout with loop definition query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099520#M62472</link>
    <description>&lt;P&gt;ah I think I made a silly error, I'm unable to test this.&lt;/P&gt;&lt;P&gt;side note - why are you using mxd.save() and why have you got outFile variable created then overwritten 2 lines later?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:

    arcpy.RefreshActiveView()
    mxd.save()   
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    new_extent = layerFile.getExtent()
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title"):      
    
        if elm.name == "title":
           elm.text = wl

    arcpy.RefreshActiveView()
    outFile = os.path.join(imgLocation, wl + ".pdf")
    arcpy.AddMessage(outFile)
    outFile = imgLocation + "\\" + wl + ".pdf"   
    arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Sep 2021 19:26:51 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2021-09-17T19:26:51Z</dc:date>
    <item>
      <title>export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098354#M62403</link>
      <description>&lt;P&gt;i would like to complete my script to zoom to layer per definition and export pdf but the script give me bugs when it export pdf and not zooming can any help me&lt;/P&gt;&lt;P&gt;import arcpy, os&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;BR /&gt;df = arcpy.mapping.ListDataFrames(mxd)[0]&lt;/P&gt;&lt;P&gt;layerName = arcpy.GetParameterAsText(0)&lt;BR /&gt;fieldName = arcpy.GetParameterAsText(1)&lt;BR /&gt;imgLocation = arcpy.GetParameterAsText(2)&lt;BR /&gt;whereList = []&lt;/P&gt;&lt;P&gt;layerFile = arcpy.mapping.Layer(layerName)&lt;BR /&gt;layerFile.definitionQuery = ""&lt;/P&gt;&lt;P&gt;print df.name&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(layerName, fieldName) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;for item in row:&lt;BR /&gt;whereList.append(item)&lt;/P&gt;&lt;P&gt;for wl in whereList:&lt;BR /&gt;layerFile.definitionQuery = fieldName + "= '" + wl + "'"&lt;BR /&gt;df = layerFile.getExtent()&lt;BR /&gt;df.extent = outFile&lt;BR /&gt;outFile = imgLocation + "\\" + wl + ".pdf"&lt;/P&gt;&lt;P&gt;arcpy.mapping.ExportToPDF(mxd, outFile)&lt;/P&gt;&lt;P&gt;del mxd&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 09:08:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098354#M62403</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-15T09:08:49Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098385#M62408</link>
      <description>&lt;P&gt;This bit makes no sense&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#variables are all messed up
for wl in whereList:
  layerFile.definitionQuery = fieldName + "= '" + wl + "'"
  df = layerFile.getExtent()
  df.extent = outFile
  outFile = imgLocation + "\\" + wl + ".pdf"

#should be
for wl in whereList:
  layerFile.definitionQuery = fieldName + "= '" + wl + "'"
  new_extent = layerFile.getExtent()
  df.extent = new_extent
  arcpy.RefreshActiveView()
  outFile = imgLocation + "\\" + wl + ".pdf"&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 Sep 2021 12:52:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098385#M62408</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-15T12:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098440#M62409</link>
      <description>&lt;P&gt;the extent run very well but still getting error to export pdf&amp;nbsp;&lt;/P&gt;&lt;P&gt;File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1156, in ExportToPDF&lt;BR /&gt;layout.exportToPDF(*args)&lt;BR /&gt;AttributeError: Invalid destination path&lt;/P&gt;&lt;P&gt;Failed to execute (Script)&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 14:15:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098440#M62409</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-15T14:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098461#M62410</link>
      <description>&lt;P&gt;what path are you supplying for imgLocation?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I prefer to use the os.path.join() method when constructing paths:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 14:28:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098461#M62410</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-15T14:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098486#M62411</link>
      <description>&lt;P&gt;it worked thanks but when it get extent , the extent 100% i want to customize it to 115% to&amp;nbsp; make the feature fully visible&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 15:09:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098486#M62411</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-15T15:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098508#M62412</link>
      <description>&lt;P&gt;probably adjust the scale factor e.g.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df.scale *= 1.15&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 Sep 2021 15:32:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098508#M62412</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-15T15:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098686#M62418</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture2.JPG" style="width: 861px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/23077i465878381749AC75/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture2.JPG" alt="Capture2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P class="lia-align-center"&gt;&lt;STRONG&gt;&amp;nbsp;this my layout and after i export the layout to pdf after i export it looked this&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 492px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/23078i75A447AB430994BB/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;and the size page is A1&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 21:34:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098686#M62418</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-15T21:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098850#M62419</link>
      <description>&lt;P&gt;&lt;STRONG&gt;how i export the pdf from the layout ?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 09:38:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1098850#M62419</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-16T09:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099318#M62441</link>
      <description>&lt;P&gt;Can you share your final code?&lt;/P&gt;&lt;P&gt;You're saying that the second image is the pdf produced?&lt;/P&gt;&lt;P&gt;Are you sure you're specifying the same MXD which contains the layout?&lt;/P&gt;&lt;P&gt;The layout should be used as default in&amp;nbsp;&lt;SPAN&gt;arcpy.mapping.ExportToPDF(mxd, outFile), but maybe try this just in case:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and what if you try JPEG or PNG etc. ?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;outFile = os.path.join(imgLocation, wl + ".jpg")
arcpy.mapping.ExportToJPEG(mxd, outFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 11:38:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099318#M62441</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T11:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099336#M62445</link>
      <description>&lt;P&gt;&lt;STRONG&gt;you helped me more thanks this the final script by&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&lt;A href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692" target="_self"&gt;DavidPike&lt;/A&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp;i want use the dynamic table and this not in armap this onlyy in arcgis pro so i want convert my final script to arcgis pro&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy, os&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/P&gt;&lt;P&gt;df = arcpy.mapping.ListDataFrames(mxd)[0]&lt;/P&gt;&lt;P&gt;layerName = arcpy.GetParameterAsText(0)&lt;BR /&gt;fieldName = arcpy.GetParameterAsText(1)&lt;BR /&gt;imgLocation = arcpy.GetParameterAsText(2)&lt;BR /&gt;whereList = []&lt;/P&gt;&lt;P&gt;layerFile = arcpy.mapping.Layer(layerName)&lt;BR /&gt;layerFile.definitionQuery = ""&lt;/P&gt;&lt;P&gt;print df.name&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(layerName, fieldName) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;for item in row:&lt;BR /&gt;whereList.append(item)&lt;/P&gt;&lt;P&gt;for wl in whereList:&lt;BR /&gt;layerFile.definitionQuery = fieldName + "= '" + wl + "'"&lt;BR /&gt;&lt;BR /&gt;new_extent = layerFile.getExtent()&lt;BR /&gt;df.extent = new_extent&lt;BR /&gt;df.scale *= 1.5&lt;BR /&gt;arcpy.RefreshActiveView()&lt;BR /&gt;outFile = os.path.join(imgLocation, wl + ".pdf")&lt;BR /&gt;arcpy.AddMessage(outFile)&lt;BR /&gt;outFile = imgLocation + "\\" + wl + ".pdf"&lt;BR /&gt;arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")&lt;/P&gt;&lt;P&gt;del mxd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 13:27:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099336#M62445</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T13:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099345#M62446</link>
      <description>&lt;P&gt;Did what I say work? If so what was the issue and what solved it?&lt;/P&gt;&lt;P&gt;Re converting it to Pro, it will be more than a simple changing of a few things, as there's a different mapping class, camera objects etc.&amp;nbsp; I'd recommend putting this thread to rest and posting an entirely new question.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 13:45:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099345#M62446</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T13:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099372#M62447</link>
      <description>&lt;P&gt;thanks i will do it but befor i want to add a dynamic text in arcmap each layout want the dynamic lebel change i tried to this but not organized my code&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy, os&lt;/P&gt;&lt;P&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/P&gt;&lt;P&gt;df = arcpy.mapping.ListDataFrames(mxd)[0]&lt;/P&gt;&lt;P&gt;layerName = arcpy.GetParameterAsText(0)&lt;BR /&gt;fieldName = arcpy.GetParameterAsText(1)&lt;BR /&gt;imgLocation = arcpy.GetParameterAsText(2)&lt;BR /&gt;whereList = []&lt;/P&gt;&lt;P&gt;layerFile = arcpy.mapping.Layer(layerName)&lt;BR /&gt;layerFile.definitionQuery = ""&lt;/P&gt;&lt;P&gt;print df.name&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(layerName, fieldName) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;for item in row:&lt;BR /&gt;whereList.append(item)&lt;BR /&gt;mxd.activeView = "PAGE_LAYOUT"&lt;BR /&gt;&lt;STRONG&gt;for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): &lt;/STRONG&gt;&lt;BR /&gt;for wl in whereList:&lt;BR /&gt;&lt;STRONG&gt;if elm.text == "title":&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;elm.text = elm.text.replace(elm,wl)&lt;/STRONG&gt;&lt;BR /&gt;arcpy.RefreshActiveView()&lt;BR /&gt;mxd.save()&lt;BR /&gt;layerFile.definitionQuery = fieldName + "= '" + wl + "'"&lt;BR /&gt;new_extent = layerFile.getExtent()&lt;BR /&gt;df.extent = new_extent&lt;BR /&gt;df.scale *= 1.5&lt;BR /&gt;arcpy.RefreshActiveView()&lt;BR /&gt;outFile = os.path.join(imgLocation, wl + ".pdf")&lt;BR /&gt;arcpy.AddMessage(outFile)&lt;BR /&gt;outFile = imgLocation + "\\" + wl + ".pdf"&lt;BR /&gt;arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")&lt;/P&gt;&lt;P&gt;del mxd&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 14:34:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099372#M62447</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T14:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099379#M62448</link>
      <description>&lt;P&gt;If you properly format your code I might be able to assist.&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 14:55:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099379#M62448</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T14:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099414#M62452</link>
      <description>&lt;LI-CODE lang="python"&gt;import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)
#the code blow for lebel Dynamic in the layout
mxd.activeView = "PAGE_LAYOUT"
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):      
    for wl in whereList:
        if elm.text == "title":
           elm.text = elm.text.replace(elm,wl)
        arcpy.RefreshActiveView()
        mxd.save()   
        layerFile.definitionQuery = fieldName + "= '" + wl + "'"
        new_extent = layerFile.getExtent()
        df.extent = new_extent
        df.scale *= 1.5
        arcpy.RefreshActiveView()
        outFile = os.path.join(imgLocation, wl + ".pdf")
        arcpy.AddMessage(outFile)
        outFile = imgLocation + "\\" + wl + ".pdf"   
        arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")

del mxd&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 17 Sep 2021 16:09:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099414#M62452</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T16:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099422#M62454</link>
      <description>&lt;P&gt;Having the for loop for the text element as the initial loop is a bit off imho. also note your element would have to be named 'title' (in the text element properties on your layout there is a name box where you can specify this)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    
for wl in whereList:

    arcpy.RefreshActiveView()
    mxd.save()   
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    new_extent = layerFile.getExtent()
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):      
    
        if elm.text == "title":
           elm.text = wl

    arcpy.RefreshActiveView()
    outFile = os.path.join(imgLocation, wl + ".pdf")
    arcpy.AddMessage(outFile)
    outFile = imgLocation + "\\" + wl + ".pdf"   
    arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 16:27:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099422#M62454</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T16:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099492#M62467</link>
      <description>&lt;P&gt;i made test and change&amp;nbsp; the lebel to "title" as you see in the screen shot but the pdf exportefd without chang&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;e the dynamic lebel can you retest the code and see it again&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/23293i2002EE7293FCC3CA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;my final look code is this&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:

    arcpy.RefreshActiveView()
    mxd.save()   
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    new_extent = layerFile.getExtent()
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):      
    
        if elm.text == "title":
           elm.text = wl

    arcpy.RefreshActiveView()
    outFile = os.path.join(imgLocation, wl + ".pdf")
    arcpy.AddMessage(outFile)
    outFile = imgLocation + "\\" + wl + ".pdf"   
    arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 17 Sep 2021 18:24:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099492#M62467</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T18:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099520#M62472</link>
      <description>&lt;P&gt;ah I think I made a silly error, I'm unable to test this.&lt;/P&gt;&lt;P&gt;side note - why are you using mxd.save() and why have you got outFile variable created then overwritten 2 lines later?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:

    arcpy.RefreshActiveView()
    mxd.save()   
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    new_extent = layerFile.getExtent()
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title"):      
    
        if elm.name == "title":
           elm.text = wl

    arcpy.RefreshActiveView()
    outFile = os.path.join(imgLocation, wl + ".pdf")
    arcpy.AddMessage(outFile)
    outFile = imgLocation + "\\" + wl + ".pdf"   
    arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 19:26:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099520#M62472</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T19:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099533#M62474</link>
      <description>&lt;P&gt;&lt;STRONG&gt;firstly i would like to thank a lot&amp;nbsp; for your effort&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;i made in mxd save and overwrite beacouse the code write from the field have char like )(.\ and the code run out while was running so&amp;nbsp;&lt;/P&gt;&lt;P&gt;saving mxd to know the last extent while the cod was run&amp;nbsp;&lt;/P&gt;&lt;P&gt;what iam thinking now to put the function .strip() int outfile name to clear char like this ()\|?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 20:06:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099533#M62474</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T20:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099534#M62475</link>
      <description>&lt;P&gt;the code worked&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 20:07:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099534#M62475</guid>
      <dc:creator>MahmoudHegazy</dc:creator>
      <dc:date>2021-09-17T20:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: export map layout with loop definition query</title>
      <link>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099554#M62479</link>
      <description>&lt;P&gt;.strip() would only remove trailing and leading stuff.&amp;nbsp; Probably best to do something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;name = ''
for char in wl:
  if char.isalpha():
    name+=char
outFile = imgLocation + '\\' + name + '.pdf'&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 17 Sep 2021 21:03:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/export-map-layout-with-loop-definition-query/m-p/1099554#M62479</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-17T21:03:23Z</dc:date>
    </item>
  </channel>
</rss>

