Script: export DDP to PDF but sorting by a field in attribute table of Grid Index

1907
2
08-14-2013 11:05 AM
YanetCuddus
New Contributor
I am new to scripting and I hit a wall at every turn. What am I doing wrong?

There are two parameters:  1) Select Map Document to Print,   2) Select Output File Location

import arcpy, os, sys, string

#Read input parameters from script tool
mxdPath = arcpy.GetParameterAsText(0)
outputlocation = arcpy.GetParameterAsText(1)

#Reference the map and the data driven page object
mxd = arcpy.mapping.MapDocument(mxdPath)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
   mxd.dataDrivenPages.currentPageID = pageNum

# Get the row from the attribute table of the index layer
  row = mxd.dataDrivenPages.pageRow

  # Get the value from the field called PageName
  name = row.getValue("PageName")

  # Concatenate into whole path name
  outputName = outputlocation + "\\" + name + "_ArcMap" + ".pdf" 

  # Export to PDF
ddp.exportToPDF (outputName, CURRENT",1,"PDF_SINGLE_FILE",300,"BEST","RGB",1,"ADAPTIVE","RASTERIZE_BITMAP",0,1,"LAYERS_ONLY",1,80)

# Clean up
del mxd


Untimately I want to build on it and do a filter so I only print pages that pass the query "Type" LIKE 'Vertical Map'.

I have tried it a couple of ways and I am lost in the weeds.
0 Kudos
2 Replies
JimCousins
MVP Regular Contributor
Without doing any debugging, the export to PDF line needs an additional quote at the front of CURRENT.
Jim
0 Kudos
MattSayler
Occasional Contributor II
For a project I did, I added a numeric "PageOrder" field to the Index Layer. I set it as the DDP Sort field and just mirrored the DDP page numbers. I then use a Search Cursor to to filter and sort the pages.

So for an index layer something like:
[TABLE="class: grid, width: 30%"]ObjectID
PageName
Type
PageOrder
Shape
5
Page_1
Type1
1
14
Page_2
Type2
2
6
Page_3
Type1
3

[/TABLE]


Something like this:
mainPDF = r"c:\yourPath\pdfname.pdf"
pdf = arcpy.mapping.PDFDocumentCreate(mainPDF) #Generate pdf to hold the pages

#create a Search Cursor that filters to the pages you want & sorts on a field
pageList = arcpy.SearchCursor(indexLayer, "\"Type\" = 'Type1'", None, None, "PageOrder A;")

for page in pageList:
    pageNum = page.getValue("PageOrder") 
    mxd.dataDrivenPages.currentPageID = pageNum #set the DDP page number

    tempPDF = "c:\\tempPath\\{0}.pdf".format(str(page.getValue("PageName")))
    arcpy.mapping.ExportToPDF(mxd, tempPDF) #generate pdf of page

    mainPDF.appendPages(tempPDF) #add the page to the main pdf

mainPDF.saveAndClose()


NOTE: This is for 10.0. If you're on 10.1 or newer there are new 'da' cursors (i.e. arcpy.da.SearchCursor(...)) which are more efficient. Be aware that the parameters are set up a little differently though. Also, generally a good idea to check if a pdf already exists before creating it. Left that out as not to detract.

I suspect there are more elegant ways to do this though. The way I do it, if I insert an index feature somewhere in the middle of the pack, I have to manually shift the "PageOrder" values to accomidate. Not difficult, but it's manual and one more step to remember to do.
0 Kudos