import arcpy, os # Create an output directory variable # outDir = r"C:\Working_Directory\Combined" # Create a new, empty pdf document in the specified output directory # finalpdf_filename = outDir + r"\FinalStorm.pdf" if os.path.exists(finalpdf_filename): os.remove(finalpdf_filename) finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename) # Add the title page to the pdf # # finalPdf.appendPages(r"G:\CamarilloGIS\Projects\Atlases\Storm_Drain\2007\CoverSheet.pdf") # Add the index map to the pdf # # finalPdf.appendPages(r"G:\CamarilloGIS\Projects\Atlases\Storm_Drain\2007\GridIndex.pdf") # Create Facing Pages for the map book # Create pages for left-hand side of the book # mxdPathLeft = r"G:\CamarilloGIS\Projects\PublicWorks\STORMWATER\Updates\Even11x17_Aerial.mxd" tempMapLeft = arcpy.mapping.MapDocument(mxdPathLeft) tempDDPLeft = tempMapLeft.dataDrivenPages # Loop creates individual pdf's for odd numbered pages # #for pgNumLeft in range(1, tempDDPLeft.pageCount + 1,): # temp_filename = r"C:\Working_Directory\L_" + \ # str(pgNumLeft) + ".pdf" # if os.path.exists(temp_filename): # os.remove(temp_filename) # tempDDPLeft.exportToPDF(temp_filename, "RANGE", pgNumLeft) # # Create pages for right-hand side of the book # mxdPathRight = r"G:\CamarilloGIS\Projects\PublicWorks\STORMWATER\Updates\Odd11x17_Aerial.mxd" tempMapRight = arcpy.mapping.MapDocument(mxdPathRight) tempDDPRight = tempMapRight.dataDrivenPages # Loop creates individual pdf's for even numbered pages # #for pgNumRight in range(2, tempDDPRight.pageCount + 1,): # temp_filename = r"C:\Working_Directory\R_" + \ # str(pgNumRight) + ".pdf" # if os.path.exists(temp_filename): # os.remove(temp_filename) # tempDDPRight.exportToPDF(temp_filename, "RANGE", pgNumRight) # Append right and left-hand pages together in proper order i = 1 #set i equal to 1 to start with the first PDFs for pgNum in range(1, tempDDPLeft.pageCount + 1): print "Page", pgNum, "of", tempDDPLeft.pageCount tempPDF = r"C:\Working_Directory\combined" + os.sep + str(pgNum) + ".pdf" finalPdf.appendPages(r"C:\Working_Directory\L_" + str(i) + ".pdf") #appends the first PDF from the odds finalPdf.appendPages(r"C:\Working_Directory\R_" + str(i) + ".pdf") #appends the first PDF from the evens i = i + 1 #adds 1 to i so that it will append PDFs number 2 next, then 3, 4, and so on through 48finalPdf.appendPages(tempPDF) # Update the properties of the final pdf # finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS", pdf_layout="SINGLE_PAGE") # Save your result # finalPdf.saveAndClose() # Delete variables # del finalPdf, mxdPathLeft, mxdPathRight, tempDDPLeft, tempDDPRight, tempMapLeft, tempMapRight, tempPDF
print r"C:\Working_Directory\L_" + str(i) + ".pdf" print r"C:\Working_Directory\R_" + str(i) + ".pdf"
finalPdf.appendPages("C:\\Working_Directory\\L_%s.pdf" % str(i))
finalPdf.appendPages(r"C:\\Working_Directory\\R_%s.pdf" % str(i)) #appends the first PDF from the evens
# Delete variables # del finalPdf, mxdPathLeft, mxdPathRight, tempDDPLeft, tempDDPRight, tempMapLeft, tempMapRight, tempPDF
r"C:\Working_Directory\R_%s.pdf" % i
r"C:\Working_Directory\R_{0}.pdf".format(i)
To anyone using this script, I only changed the file locations and was still getting the error messsage that Tom was getting above (I'm using version 10.2.2). After hours of browsing websites/blogs for information that might help, I found that changing the few instances of "pgNum(Left/Right)" to "pageNum(Left/Right)" solved all my problems!
Can you elaborate on that? I don't see how simply changing a variable name could have solved anything, unless it was otherwise originally specified.
I'm not sure why it worked, as I'm a beginner at Python. pgNum isn't a variable I defined. Based on the error message I was getting (exactly the same as in the original post I replied to), and errors I could avoid by commenting out sections of my script, I assumed there was something off with the loops, so I started investigating. I looked at various other similar scripts on blog sites and noticed that some of them were using "page" instead of "pg", so I decided to try it and it worked. The small difference may just be based on the version of Python or Arc I'm using....
My guess is you are using the samples from this blog post Combining Data Driven Pages with Python and arcpy.mapping | ArcGIS Blog
They show samples using both variables, but I'm wondering if pgNum is a reserved word.
Here is where I got the original script. http://resources.arcgis.com/en/help/main/10.2/index.html#//00s90000002p000000