AnsweredAssumed Answered

Python - Using data driven pages to select features from another layer to get attributes to text

Question asked by deleted-user-MS0lE1F_0-sy on Jun 8, 2015
Latest reply on Nov 23, 2015 by deleted-user-MS0lE1F_0-sy

I am trying to loop through exporting data drive pages, select data from another layer based on the page getting exported, and write a row of the results of the selection to a text element.  Each page should have different results in text element.  For each page getting exported I I want to select all the parcels within the current page/feature getting exported and write the results to the text element.  What I have so far only writes the results for the first page on every page.  When it runs it does seem to select the parcels per page while exporting, but it does not re-write the results to the text element.  Not sure what I am missing?


# Always run script in the mxd you are working on.
mxd = arcpy.mapping.MapDocument("CURRENT")

# Overwrites any existing features.
arcpy.env.overwriteOutput = True

# Uses the Dataframe called "Layers".
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# Parcel Text Element
ParcelText2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ParcelsText2")[0]

# Data Driven Pages Export Sheets
ExportSheets = arcpy.mapping.Layer("Sheets_11x17")

# Parcels
Parcels = arcpy.mapping.Layer("Parcels")

# Data Driven Pages mxd abbreviation
ddp = mxd.dataDrivenPages

# Loop through DDP's
for pageNumb in range(1, mxd.dataDrivenPages.pageCount + 1):
    ddp.currentPageID = pageNumb

    # DDP Export Sheet duplicate which has the page definition Query
    SelectedSheet = arcpy.mapping.Layer("Sheets_11x172")

    # Exports Row page
    ExportSheetsPage = ddp.pageRow.PLAN_NUMBER

    # Select Parcels withing my SelectedSheet
    arcpy.SelectLayerByLocation_management(Parcels, "WITHIN", SelectedSheet)

    # Use SearchCursor to get Parcels value and write to Parcel Text Element
    rows = arcpy.SearchCursor(Parcels)
    textValue = ""
    for row in rows:
        textValue += row.getValue("MAP_PARCEL")
    ParcelText2.text = textValue

    # Export Sheets