0

I would like to take a ArcMap mapbook and export each individual page to a PDF named for the page and/or GridID. I've never messed with Python... But I see that the arcpy.mapping module should do the trick. Doesn't seem like this should be too difficult, but I am failing...

i took Gistech007's code and modified by adding a line - pageName = mxd.dataDrivenPages.pageRow.stand_key and then instead of having str(pageNum) in the name I have str(pageName). so it looks like this now:

import arcpy
 print "Setting Map Document. . ."
  mxd = arcpy.mapping.MapDocument("CURRENT")
  for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum  
  pageName = mxd.dataDrivenPages.pageRow.index
  print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str (mxd.dataDrivenPages.pageCount))
  arcpy.mapping.ExportToPDF(mxd, r"D:\Temp\Page_" + str(pageName) + ".pdf")
 del mxd

Where "index" in the line - pageName = mxd.dataDrivenPages.pageRow.index is a field in my table. This runs and works just fine, but inst what i am after. I need the PDF to be named with only the str(pageName) and no prefix of "Page_". If I remove the "Page_" from the code it throws an error... How can i create the PDF and have it named using only the value from pageName?

1
  • If by mapbook you are referring to Data Driven Pages, then all you need in exporting to pdf is on the pages tab have all pages selected and multiple pdfs in the bottom drop down. But if you want to use arcpy see blogs.esri.com/esri/arcgis/2010/12/14/…
    – John
    Commented Feb 2, 2015 at 17:41

1 Answer 1

2

If you are indeed using data driven pages, you can use the following code to export each page as an individual PDF:

    import arcpy
print "Setting Map Document. . ."
mxd = arcpy.mapping.MapDocument(r"D:\Folder\YourMapDocument.mxd")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPDF(mxd, r"D:\MB\Page_" + str(pageNum) + ".pdf")
del mxd

Just ensure that you enter the path to and the name of the Map Document you are working with. You can also play around with the expression in the second last line to name the individual pages the way you want.

For help you can look here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000030000000

The code would need to be run from IDLE or if you want to run it straight from the python window within the MXD you are working with, just change the third line of the code to this:

mxd = arcpy.mapping.MapDocument("CURRENT")
2
  • 007. thanks for the help your code works great, but i misspoke when i said i wanted the name to be based on the page number. What i really need is it to be the index field of the index layer. I managed to alter your code and can get it in there, but it seems I cant do away with the filename prefix. i did this by adding the line "pageName = mxd.dataDrivenPages.pageRow.index" then on line 8 instead of str(pageNum) i used str(pageName). However if i alter r"D:\Temp\Page_" by removing the Page_ it wont work. Does one have to have a file prefix?
    – Todd8665
    Commented Feb 3, 2015 at 16:35
  • Glad to know the code helped! Could you please post the error you're getting? It might help in pin pointing the issue. Also, you might want to try replacing the raw string. Instead of r"D:\Temp\Page_", try: "D://Temp//" + str(pageName) + ".pdf") and see if that works for you. Let me know!
    – gistech007
    Commented Feb 5, 2015 at 15:24

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.