In ArcMap 10.4 I have an index layer in DDP enabled mxd. The index layer has 50 unique Name values (used as the PageName in DDP) but there are more rows with the same Name value providing different scales, spatial extents etc. I'm looking for an ArcPy script to export pdf files with all pages of the same Name.
Example:
Name Scale
A 5000
A 600
B 25000
B 5000
B 2500
...
Result:
A.pdf (2 pages)
B.pdf (3 pages)
...
Would you be so kind to provide some hint? Apparently, I'm stuck in the process of correctly defining the 'for' loop.
So far I tried to use the modified code from Cindy Jayakumar https://gis.stackexchange.com/a/71702/93026, which was closest to what I found:
import arcpy
import os
mxd = arcpy.mapping.MapDocument("CURRENT")
output_folder = r"C:\temp"
ddp_lyr = mxd.dataDrivenPages.indexLayer
pdf_field = "Name"
#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:
arcpy.mapping.PDFDocumentCreate(os.path.join(output_folder,pdfName + "_inv.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
The above code does what I need to some extent as from some reason does not export the pdf files for all unique values in the "Name" field. I was not able to identify why some have been exported and some not. I know there is much easier way to do this as 2 years ago I already had the code working (though not saving it).