0

Could you please help me with this error:

Traceback (most recent call last): File "C:\DDPeX\Export_GroupPDF.py", line 19, in finalPdf.appendPages(tempPdf) NameError: name 'finalPdf' is not defined Failed to execute (CFAsGroupDDP).

import arcpy
import os
mxd = arcpy.mapping.MapDocument("CURRENT")
output_folder = arcpy.GetParameterAsText(0)
ddp_lyr = mxd.dataDrivenPages.indexLayer
pdf_field = "CFA"

#Get a list of unique values to group by
pdf_list = set([row[0] for row in arcpy.da.SearchCursor(ddp_lyr,pdf_field)])

for pdfName in pdf_list:
    finalPdf = arcpy.mapping.PDFDocumentCreate(os.path.join(output_folder,pdfName + ".pdf"))
    for i in range(1, mxd.dataDrivenPages.pageCount + 1):
        mxd.dataDrivenPages.currentPageID = i
        cur = str(mxd.dataDrivenPages.pageRow.getValue(pdf_field))
        if cur == pdfName:
            tempPdf = os.path.join(output_folder,"temp_" + cur + str(i) + ".pdf")
            arcpy.mapping.ExportToPDF(mxd, tempPdf)
            finalPdf.appendPages(tempPdf)
            os.remove(tempPdf)
            finalPdf.saveAndClose()
            del finalPdf, tempPdf
  • Perhaps include a `print pdf_list' on the line after you set it to make sure that it is what you expect. – PolyGeo Sep 18 '13 at 10:08
2

You are deleting your finalPdf before you finishing looping through the data driven pages. Move the delete statement to the same level as the range loop.

for i in range(....):
   #do stuff

del finalPdf

Your Answer

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

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