I have made a script that iterates through a set of Data Driven Pages. Right now I have it that just the ID number of the data driven page is used in the file name to distinguish between each export. What I'm looking to do is instead of using the page ID number, I want to use an attribute from a specific field in the data driven page shapefile.

How do I accomplish this?

I've included my script below.

import arcpy

inputList = arcpy.GetParameterAsText(0)
bckGrndLyrs = arcpy.GetParameterAsText(1)
outputLocation = arcpy.GetParameterAsText(2)
exportDPI = arcpy.GetParameterAsText(3)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyrs in arcpy.mapping.ListLayers(mxd, "", df):
    if lyrs.name in inputList:
        lyrs.visible = False
    elif lyrs.name in bckGrndLyrs:
        lyrs.visible = True  
        lyrs.visible = False

for inFC in arcpy.mapping.ListLayers(inputList, "", df):
    if inFC.name in inputList:
        arcpy.AddMessage("Activating " + inFC.name + " Layer")
        inFC.visible = True
        for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
          mxd.dataDrivenPages.currentPageID = pageNum
          for titleItem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
              if titleItem.name == 'EditedTitle':
                arcpy.AddMessage("...Changing title to " + inFC.name + " " + str(pageNum))
                titleItem.text = inFC.name 
          arcpy.AddMessage("......Refreshing Dataframe")
          arcpy.AddMessage(".........Exporting " + inFC.name + " Page " + str(pageNum) + " as PDF")
          arcpy.mapping.ExportToPDF(mxd, outputLocation + " " + inFC.name + " " + str(pageNum) + ".pdf", resolution=exportDPI)
        inFC.visible = False

del mxd

1 Answer 1


This code worked for me

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
pageName = mxd.dataDrivenPages.pageRow.CoupeID3
arcpy.mapping.ExportToTIFF(mxd, r"C:\Temp\image" + str(pageName) + ".tif", df, df_export_width=1800, df_export_height=1500, geoTIFF_tags=True)

Where CoupeID3 is whatever you want

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.