Select to view content in your preferred language

export map layout with loop definition query

4055
22
Jump to solution
09-15-2021 02:08 AM
MahmoudHegazy
Regular Contributor

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

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:
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
df = layerFile.getExtent()
df.extent = outFile
outFile = imgLocation + "\\" + wl + ".pdf"

arcpy.mapping.ExportToPDF(mxd, outFile)

del mxd

Tags (4)
0 Kudos
7 Solutions

Accepted Solutions
DavidPike
MVP Frequent Contributor

This bit makes no sense

#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"

View solution in original post

MahmoudHegazy
Regular Contributor

the extent run very well but still getting error to export pdf 

File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1156, in ExportToPDF
layout.exportToPDF(*args)
AttributeError: Invalid destination path

Failed to execute (Script)

View solution in original post

0 Kudos
DavidPike
MVP Frequent Contributor

what path are you supplying for imgLocation? 

I prefer to use the os.path.join() method when constructing paths:

outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)

 

View solution in original post

DavidPike
MVP Frequent Contributor

probably adjust the scale factor e.g.

df.scale *= 1.15

View solution in original post

DavidPike
MVP Frequent Contributor

Can you share your final code?

You're saying that the second image is the pdf produced?

Are you sure you're specifying the same MXD which contains the layout?

The layout should be used as default in arcpy.mapping.ExportToPDF(mxd, outFile), but maybe try this just in case:

 

arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")

 

and what if you try JPEG or PNG etc. ?

outFile = os.path.join(imgLocation, wl + ".jpg")
arcpy.mapping.ExportToJPEG(mxd, outFile)

 

View solution in original post

DavidPike
MVP Frequent Contributor

ah I think I made a silly error, I'm unable to test this.

side note - why are you using mxd.save() and why have you got outFile variable created then overwritten 2 lines later?

 

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

 

 

View solution in original post

DavidPike
MVP Frequent Contributor

.strip() would only remove trailing and leading stuff.  Probably best to do something like this:

name = ''
for char in wl:
  if char.isalpha():
    name+=char
outFile = imgLocation + '\\' + name + '.pdf'

View solution in original post

22 Replies
DavidPike
MVP Frequent Contributor

This bit makes no sense

#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"
MahmoudHegazy
Regular Contributor

the extent run very well but still getting error to export pdf 

File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1156, in ExportToPDF
layout.exportToPDF(*args)
AttributeError: Invalid destination path

Failed to execute (Script)

0 Kudos
DavidPike
MVP Frequent Contributor

what path are you supplying for imgLocation? 

I prefer to use the os.path.join() method when constructing paths:

outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)

 

MahmoudHegazy
Regular Contributor

it worked thanks but when it get extent , the extent 100% i want to customize it to 115% to  make the feature fully visible

0 Kudos
DavidPike
MVP Frequent Contributor

probably adjust the scale factor e.g.

df.scale *= 1.15
MahmoudHegazy
Regular Contributor

how i export the pdf from the layout ?

0 Kudos
MahmoudHegazy
Regular Contributor

Capture2.JPG

 this my layout and after i export the layout to pdf after i export it looked this 

Capture.JPG

and the size page is A1

0 Kudos
DavidPike
MVP Frequent Contributor

Can you share your final code?

You're saying that the second image is the pdf produced?

Are you sure you're specifying the same MXD which contains the layout?

The layout should be used as default in arcpy.mapping.ExportToPDF(mxd, outFile), but maybe try this just in case:

 

arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")

 

and what if you try JPEG or PNG etc. ?

outFile = os.path.join(imgLocation, wl + ".jpg")
arcpy.mapping.ExportToJPEG(mxd, outFile)

 

MahmoudHegazy
Regular Contributor

you helped me more thanks this the final script by DavidPike
 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 

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:
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

 

0 Kudos