2

I have a set of data driven pages and I would like to have a string parameter in the tool that allows me to export specific pages from the set of data driven pages. An example would be if I had a set of 50 pages, I could just enter "1-10, 20, 35-40" and it would export only those pages.

import arcpy

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

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  
    else:
        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):
          if str(pageNum) in pageRng:
              mxd.dataDrivenPages.currentPageID = pageNum
              blockName = mxd.dataDrivenPages.pageRow.Block
              for titleItem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
                  if titleItem.name == 'EditedTitle':
                    arcpy.AddMessage("...Changing title to " + inFC.name)
                    titleItem.text = inFC.name + str(pageNum)
              arcpy.AddMessage("......Refreshing Dataframe")
              arcpy.RefreshTOC()
              arcpy.RefreshActiveView()
              arcpy.AddMessage(".........Exporting " + inFC.name + " " + blockName + " as PDF")
              arcpy.mapping.ExportToPDF(mxd, outputLocation + "/" + inFC.name + " " + blockName + ".pdf", resolution=exportDPI)
        inFC.visible = False

del mxd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.