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.


Name   Scale
A      5000
A      600
B      25000
B      5000
B      2500


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)
    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).

  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. What have you tried? A good question should include some degree of research and attempt. Please edit your question to include any code you've attempted, and details on what happens when you try it including any error messages in full. There are many Q&As here regarding arcpy for exporting PDFs from DDP - that would be a good place to search first.
    – Midavalo
    Commented Mar 8, 2017 at 17:39
  • 2
    Once you have a script that mostly do what you want, then come back and ask focused questions regarding anything that doesn't work.
    – Midavalo
    Commented Mar 8, 2017 at 17:41
  • The two dead links you provided were just mirrors of the question you got your code from - gis.stackexchange.com/questions/71631/…
    – Midavalo
    Commented Mar 9, 2017 at 16:57
  • Can you please expand on what you mean by "it omits some Names"? Can you provide a bit more detail?
    – Midavalo
    Commented Mar 9, 2017 at 16:58

1 Answer 1


At the end (and after some hours of debugging) I was able to create simple code which does the export.

I'll set another example:

Sites   River
A       Nile
A       Nile
A       Nile
B       Nile
B       Nile
C       Amazon
C       Amazon


import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
ddp = mxd.dataDrivenPages
indexLayer = ddp.indexLayer
output_folder = r"C:\temp"
select = set(row[0] for row in arcpy.da.SearchCursor(indexLayer, 'Sites'))

for site in select:
    where_clause = '"Sites" = ' + "'%s'" %site
    arcpy.SelectLayerByAttribute_management (indexLayer, "NEW_SELECTION", where_clause)
    riv = set(row[0] for row in arcpy.da.SearchCursor(indexLayer, 'River'))
    ddp.exportToPDF(r"C:\Temp\\" + str(site) + "_" + str(riv)[7:-3] + ".pdf", "SELECTED" )
del mxd

The stripping parameter str(riv)[7:-3] in the definition of the exported pdf name had to be added to delete the 'extra' characters "set(['*'])".

Before deletion:


After deletion:


I don't know why variable riv returns this extended format, while variable site is pure string value, however, the script works fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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