Good morning,I am sorry to be such a needy scripter. If you don't have the time to look at this, I understand.I used the last code that you wrote and put 3 townships in my list. All 3 townships should result in pages with data in the .pdf. The script only outputs the first map, and then shows that the next two maps have zero pages. I have attached a map package. This contains the pole data to be displayed, the index layer, and the pole layer to be used for intersection. It also contains a back cover and 4 cover pages. I included the data for the 3 townships with data and one without (D64).Thank you# PoleDDpages.py
# Author: slw
# Date: XXXXX
# Revisions: XXXXX
# Description: Create Pole map book series using Data Driven Pages and
# insert a cover page
# Import ArcPy
import os,arcpy
# Overwrite existing file
arcpy.env.overwriteOutput = True
try:
# Set up variables
# Location of pole map .mxd document
mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd"
# Create the MapDocument object
mxd = arcpy.mapping.MapDocument(mxdDoc)
df = arcpy.mapping.ListDataFrames(mxd)[0]
layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY",df)[0]
poleLyr = arcpy.mapping.ListLayers (mxd, "Pole",df)[0]
################
# moved part of code to select grids (layer) intersecting 'Pole' features (poleLyr)
# Create temporary layers to work with the Selection,
# 1st need to assure no prior selections...the sel by loc syntax:
# SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})
# in_layer is the index layer; select_features is the pole layer
SelectBy = [layer, poleLyr]
for lyr in SelectBy:
iniCount = int(arcpy.GetCount_management(lyr).getOutput(0))
if iniCount != 0:
arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION')
# should now be properly 'initialized' with no prior selections, i.e., full complement datasets
arcpy.MakeFeatureLayer_management(layer, "surveyLyr")
arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")
# Select Layer By Location to limit to just maps with data
arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")
# Susan, check that this works, switching the selection...
# SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause})
arcpy.SelectLayerByAttribute_management("surveyLyr", "SWITCH_SELECTION")
# new code to get at OBJECTIDs to modify def query
IDs = []
rows = arcpy.SearchCursor("surveyLyr")
for row in rows:
IDs.append(row.OBJECTID)
del row, rows
subquery = '('
for each in IDs:
subquery = subquery + str(each) + ','
subquery = subquery[0:-1] + ')'
################
# Output directory for the pole maps
outDir = r"G:\GEOSPATIAL\Publishing\Pole"
# Set the workspace
arcpy.env.workspace = outDir
# List of map grids
twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\
'\"TOWNSHIP_C\" = \'C4\' AND \"RANGE_CD\" = \'7\'',\
'\"TOWNSHIP_C\" = \'B8\' AND \"RANGE_CD\" = \'4\'']
for twpName in twpList:
# need to test the twpName with the added subquery for empty return 1st
theQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery)
# create a feature layer on index with which to test theQuery for empty set
# MakeFeatureLayer_management (in_features, out_layer, {where_clause})
arcpy.MakeFeatureLayer_management(layer, "testTheCount", theQuery)
gridsToOutput = int(arcpy.GetCount_management("testTheCount").getOutput(0))
print "Output " + str(gridsToOutput) + " pages"
if gridsToOutput != 0:
# modified the definitionQuery to include the subquery exclusion clause
layer.definitionQuery = theQuery
# refresh() after changing the layer def query to 'redefine' the DDP index limits
print "Refresh DDP"
mxd.dataDrivenPages.refresh()
# The final mapbook name taken from the list
finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf"
# Create the final PDF -- which is just an empty shell right now
finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn)
# A temporary pdf file for processing
tmpPDF = outDir + "\\PoleMapPages.pdf"
# Let the user know what is happening!
print "Exporting " + twpName [16:18] + twpName [38:39]
# Export the data driven pages in the mxd to a temporary PDF
print "Exporting map pages to the temporary PDF"
ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF)
# Append the temporary pdf to the final pdf
# Cover, map pages, back cover
print "Appending Map Pages"
finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_"\
+ twpName [16:18] + twpName [38:39] + ".pdf")
finalPDF.appendPages(tmpPDF)
finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_BW8x11.pdf")
# Set properties for Adobe Reader and save PDF.
finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE")
finalPDF.saveAndClose()
# Deleting temporary layers
arcpy.Delete_management("surveyLyr")
arcpy.Delete_management("poleLyr_new")
arcpy.Delete_management("testTheCount")
except Exception as e:
print e.message
print arcpy.GetMessages(2)
# Clean up
print "Cleaning up"
# Delete the temporary PDF using the ArcPy function
if arcpy.Exists(tmpPDF):
arcpy.Delete_management(tmpPDF)
# Delete objects
del mxd, tmpPDF, ddp
# Finished message
print "Map compilation completed. Please review the final output."