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 DataFrame
s, setting the value of 8 TextElement
s, 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 TextElement
s. I've got a simplified example where all I do is
- Open the MXD
- Grab a reference to the
TextElement
- Within an endless loop:
- Set the value of the
TextElement
- Print out the time
- Set the value of the
Processing the times results in a graph like this:
...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'
print(str(datetime.datetime.now()))
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?