I created a script to get the extent of a polygon, apply it to the data frame extent, and then export the layout as a PDF. However, I'm not getting any errors but the script is printing out at a different scale and the wrong area (can't quite tell what area it is that it's printing/moving to).

Can anyone point me to what may be causing the issue and/or any documentation that might help me figure out what the problem is?

The script is as follows: (Note, there are no parameters since I'm automating a very specific workflow that utilizes templates and the same layer names each time).

# Import modules

import arcpy
from arcpy import mapping

# Allow overwriting
arcpy.env.overwriteOutput = True

# Get the X and Y information for each polygon
mxd = mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.name == "MapbookGrid":
        mapGrid = lyr

# Set the current data frame as a variable
dataframe = mapping.ListDataFrames(mxd, "*")[0]

# Gets the extent of the current data frame
frameExtent = dataframe.extent

rows = arcpy.SearchCursor(mapGrid)
shapeName = arcpy.Describe(mapGrid).shapeFieldName
for row in rows:
    feat = row.getValue(shapeName)
    Name = str(row.PageNumber)
    #print "Executing print of page: "+str(Name)
    arcpy.AddMessage("Executing print of page {0}".format(Name))
    extent = feat.extent
    pdfName = Name + ".pdf"
    pdfStore = r"\\GISAPPSER2\Engineering_GIS\ScriptWork" + pdfName
    #print pdfStore
    arcpy.AddMessage("PDF name is {0} and is stored as {1}".format(pdfName, pdfStore))
# Obtain X and Y
    XMAX = extent.XMax
    XMIN = extent.YMin
    YMAX = extent.YMax
    YMIN = extent.YMin
# Store data frame extent from polygon
    frameExtent.XMax, frameExtent.YMax = XMAX, YMAX
    frameExtent.XMin, frameExtent.YMin = XMIN, YMIN
    dataframe.extent = frameExtent
# Export to PDF
    arcpy.mapping.ExportToPDF(mxd, pdfStore)
  • 2
    You have an error in the line setting the XMIN variable. XMIN = extent.YMin – Jacob F Aug 7 '18 at 15:08
  • I should have noticed that..script now works properly. Thank you! – JackOfTales Aug 7 '18 at 16:12

The extent objects need referenced like this:

extent = feat.extent
extent.XMin = min(extent.XMin, extent.XMin)
extent.XMax = max(extent.XMax, extent.XMax)
extent.YMin = min(extent.YMin, extent.YMin)
extent.YMax = max(extent.YMax, extent.YMax)
dataframe.extent = extent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.