"Dynamic" Table using Python, DDP

425
3
12-23-2010 06:37 AM
JonathanQuinn
Esri Notable Contributor
Hello,
I have just started using Python and I'm trying to create a dynamic table using Data Driven Pages and Python. I'm using data driven pages and have a page definiton query set to display only the points that are within the page index, and only shows those records in the attribute table. I'd like to create a table in my layout that dynamically updates a text element with certain fields from the queried attribute table. So far this is what I have:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
rows = arcpy.SearchCursor("DAMS")
row = rows.next()

while row:
  stateValue = row.getValue("DAM_NAME")
  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
      if elm.text == "LayoutLegend":
          elm.text = (str(stateValue) + "\n")
  row = rows.next()
arcpy.mapping.ExportToPDF(mxd, r"C:\Documents and Settings\jquinn\Desktop\Dam Map\trial.pdf")
del mxd

In replaces "LayoutLegend" with the dam name, but it adds the first record in the feature class instead of the records in the queried attribute table. It also doesn't iterate through, adding each record to the text element.

Clearly, the code needs a bit of work, so any help would be greatly appreciated.

Thanks,
Jon
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
Hi Jon,

I believe the reason you are getting the first record (and all other records) is because you are not applying a query to your search cursor.  In this case you want to perform a SelectLayerByLocation and pass in the geometry of the data frame envelope.

1) don't put your ListLayoutElements within the loop, you only need to reference the text element once so move this line above the loop.  Depending on the complexity of the MXD, this could be a real performance problem.

2) You can remove the additional if statement if you use the wildcard parameter for ListLayoutElements, try:
      "elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "LayoutLegend")[0]

I hope this helps,
Jeff
0 Kudos
JonathanQuinn
Esri Notable Contributor
Great, thank you for getting back to me, Jeff.  I'll give those recommendations a try.
0 Kudos
JeffBarrette
Esri Regular Contributor
Jon:

Take a look at the 4th sample script provided with the DataFrame class.  It performs a SelectByLocation using the DataFrame extent as the selecting feature.  I think this is all you need.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/DataFrame/00s300000003000000/

Jeff
0 Kudos