Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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
–
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
–
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.