I'm generating a map book of about 300 pages using arcpy, instead of using the toolbox, because I need to do a bunch of scripted actions - on each page I'm setting the extent and rotation of 3 DataFrames, setting the value of 8 TextElements, and then saving the layout using arcpy.mapping.ExportToPDF.

What I'm seeing is that the loop gets exponentially slower the longer I leave it running, and I've narrowed it down to setting the value of TextElements. I've got a simplified example where all I do is

  1. Open the MXD
  2. Grab a reference to the TextElement
  3. Within an endless loop:
    • Set the value of the TextElement
    • Print out the time

Processing the times results in a graph like this: Dodgy Excel graph that looks remarkably exponential

...and it just keeps getting slower.

Has anyone seen this, and have a solution? Or do I just break up generating my map book into batches of 50-100 pages and restart the script? (which is my current workaround)

Simple example script:

import arcpy
import datetime
import os

MXD_MAPBOOK = r'E:\workspace\data\N&I\N&IMapBook_landscape2.mxd'

def findTextElement(doc, elementName):
    lstElements = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
    for elm in lstElements:
        if elm.name == elementName:
            return elm

mxd = arcpy.mapping.MapDocument(MXD_MAPBOOK)
dTime = datetime.datetime.now()
elem = findTextElement(mxd, 'baseRailLine')

while True:
    elem.text = 'Test number'
  • 2
    I've seen ArcGIS Pro's arcpy.mp encounter something that reminds me of this at gis.stackexchange.com/q/238984/115 Perhaps the limitation suspected there was inherited from ArcMap and arcpy.mapping.
    – PolyGeo
    Commented May 28, 2019 at 8:42
  • 1
    Just wildly speculating why this should happen, so not a solution. Typically edits can be undone so I guess there is an ever increasing stack of "undo's". May be that is the source of the slow down? What ever ArcPro is doing to record an undo action the logic/data structure can't handle deep iteration?
    – Hornbydd
    Commented May 28, 2019 at 9:47
  • Also this question seems related to this Q&A?
    – Hornbydd
    Commented May 28, 2019 at 9:48
  • @PolyGeo - I've definitely seen that limit before, the performance drop with a large number of layout elements is scary. I was originally searching the elements collection each time using arcpy.mapping.ListLayoutElements as above, and I figured caching the element references would fix it but no joy. Saving the PDF each time is consistent @ ~5s. @Hombydd - sounds reasonable, but arcpy.mapping doesn't really 'open' the MXD, so would it support undo?
    – Juffy
    Commented May 29, 2019 at 1:05


Your Answer

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

Browse other questions tagged or ask your own question.