1
import arcpy, os, datetime

folderPath = r"C:\path"
dateTimeStamp = time.strftime('%Y%m%d')

basename = "Flows"
mxd = arcpy.mapping.MapDocument(r"C:\path\file.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#loops through bookmarked locations and executes a PDF export
for bkmk in arcpy.mapping.ListBookmarks(mxd, "",data_frame=df):
    #going through text elements to find the map title and set it to the bookmark name
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "documentTitle"):
        elm.text = str(bkmk.name) + " - Flows"
        #set extent to bookmark extent before pdf export
        df.extent = bkmk.extent


    print basename + "_" + bkmk.name + " is exporting to PDF"
    arcpy.mapping.ExportToPDF(mxd, basename + "_" + bkmk.name + "_" + dateTimeStamp)
    print basename + "_" + bkmk.name + " successfully exported to PDF"
    print "Done exporting this folder to PDF - " +folderPath


#cleanup
del mxd

I'm receiving the following error after attempting to loop through about 10 bookmarks worth of PDF exports. The odd thing is, if I do 3-4 bookmarks at a time, it will export successfully but not if I do the full set of 10.

AttributeError: PageLayoutObject: Error in executing ExportToPDF

4
  • Can every PDF be exported if done one at a time? If so, then it would appear that you may be running out of resources. You could try making your list of bookmarks first, then deleting that MapDocument object, then within your loop of the bookmarks recreate your MapDocument object and delete it.
    – PolyGeo
    Oct 23, 2019 at 22:22
  • I've found it helpful to set a time.sleep function in the loop when exporting so that ArcMap has time to refresh in between switching bookmarks. Or after the export too. tutorialspoint.com/python/time_sleep.htm
    – dslamb
    Oct 28, 2019 at 17:21
  • @dslamb how much time to you generally give between exports or between switching bookmarks?
    – Chris
    Oct 29, 2019 at 18:32
  • @PolyGeo I ended up deleting the completed bookmarks then rerunning the script and it would work fine. Seems like it is running out of resources. I may try to do the time sleep first as what you've suggested will require quite a bit of tinkering for me. (new to arcpy and python)
    – Chris
    Oct 29, 2019 at 18:32

1 Answer 1

0

If every PDF can be exported when done one at a time, then it would appear that you may be running out of resources.

You could try making your list of bookmarks first, then deleting that MapDocument object, then within your loop of the bookmarks recreate your MapDocument object and delete it.

The kind of untested refactoring of your code that I think may work is:

import arcpy, os, datetime

folderPath = r"C:\path"
dateTimeStamp = time.strftime('%Y%m%d')

basename = "Flows"
mxd = arcpy.mapping.MapDocument(r"C:\path\file.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
bkmks = arcpy.mapping.ListBookmarks(mxd, "",data_frame=df)
del mxd

#loops through bookmarked locations and executes a PDF export
for bkmk in bkmks:
    mxd = arcpy.mapping.MapDocument(r"C:\path\file.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    #going through text elements to find the map title and set it to the bookmark name
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "documentTitle"):
        elm.text = str(bkmk.name) + " - Flows"
        #set extent to bookmark extent before pdf export
        df.extent = bkmk.extent

    print basename + "_" + bkmk.name + " is exporting to PDF"
    arcpy.mapping.ExportToPDF(mxd, basename + "_" + bkmk.name + "_" + dateTimeStamp)
    print basename + "_" + bkmk.name + " successfully exported to PDF"
    print "Done exporting this folder to PDF - " +folderPath
    #cleanup
    del mxd
0

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