I want to only display annotation from an annotation dataset that match the first three characters of the field in the shapefile used to drive a Data Driven Page (DDP) map export. (I'm running the map export through a Python script.)

Let's call the shapefile field that is used in the DDP FIELDNAME_A. Values in FIELDNAME_A have the format 360-2, 386-4, 516-2, etc.

I have an annotation dataset with a field called FIELDNAME_B which contains values of 360, 386, 516, etc.

Would it be to display annotation that matches the first three values in the DDP shapefile? For example:

DDP Shapefile FIELDNAME_A value: 360-2
Annotation data displayed from FIELDNAME_B: 360

Below are two screen captures of the attribute tables.

Data Driven Page shapefile FIELDNAME_A attribute table below

Data Driven Page shapefile FIELDNAME_A attribute table

Data Driven Page shapefile FIELDNAME_B attribute table below

Annotation FIELDNAME_B attribute table

The out of the box option for a Definition query does not seem to work, since the values in FIELDNAME_A and FIELDNAME_B do not match.

Is there a query builder or Python work around?

  • 1
    Have you tried adding a field to the shapefile and calculating its values to the first three characters of that other field, so that you can use it for that purpose?
    – PolyGeo
    Commented Sep 9, 2016 at 19:37
  • I tried doing a Spatial Join, but the output of that is a shapefile, not an annotation layer like I want. Thanks
    – Patty Jula
    Commented Sep 9, 2016 at 19:55
  • 1
    I think using Spatial Join here is getting off track. Definition query seems right but you just need to get those field values matchable like I suggested. They will need to be of the same data type too.
    – PolyGeo
    Commented Sep 9, 2016 at 20:01

1 Answer 1


Try something like this below. A couple of thing you may need to alter. I called the annotation layer the very creative name of "AnnotationLayer". Make sure you change that to your own annotation layer name. Also, if your annotation feature class is stored in a file geodatabase instead of a personal geodatabase, you may need to replace the square brackets in line 8 with quotes. And I didn't specify what you're calling your output file. I presume you already have that handled in your code.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
ddp = mxd.dataDrivenPages
annoLyr = arcpy.mapping.ListLayers(mxd, "AnnotationLayer")[0]
for i in range(1, ddp.pageCount + 1): 
  ddp.currentPageID = i
  myFieldVal = ddp.pageRow.FIELDNAME_A
  annoLyr.definitionQuery = "[FIELDNAME_B] = " + str(myFieldVal)[:3]
  arcpy.mapping.ExportToPNG(mxd, outFilePath)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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