I'm customizing an automated mapping solution using arcpy.mapping and data driven pages. Using a looping statement, I'm hardcoding the position for the mapping elements (legend, north arrow, inset map, etc. so that for each map, the mapping elements do not interfere with the area of interest. My code works for all mapping elements except for TEXT_ELEMENTS. Text element stays in the first position assigned for all of the maps.
Not quite sure why this is the case, cause I have been able to get the north arrow, a neat line, legend, scale bar, and inset map (dataframe) to move position for 16 different maps.
Sample code below showing how I'm moving the inset map and one of the text elements is shown below:
import arcpy, os
# Create an output directory variable
outDir = r"C:\Desktop\Test"
# Create a new, empty pdf document in the specified output directory
finalpdf_filename = outDir + r"\FinalMapBook.pdf"
if os.path.exists(finalpdf_filename): # Check to see if file already exists, delete if it does
os.remove(finalpdf_filename)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)
# Create a Data Driven Pages object from the mxd
mxdPath = r"C:\Desktop\Test\States.mxd"
tempMap = arcpy.mapping.MapDocument(mxdPath)
tempDDP = tempMap.dataDrivenPages
# Create objects for the layout elements that will be moving, e.g., inset data frame, scale text
dataFrame = arcpy.mapping.ListDataFrames(tempMap, "LocatorDF")[0]
for pgIndex in range(1, tempDDP.pageCount + 1, 1):
# Create a name for the pdf file you will create for each page
tempMap.dataDrivenPages.currentPageID = pgIndex
pageName = tempMap.dataDrivenPages.pageRow.Name
temp_filename = r"C:\Desktop\Test\temp_pdfs\Land_Cover_" + \
str(pgIndex) + pageName + ".pdf"
if os.path.exists(temp_filename):
os.remove(temp_filename)
#Code to modify mapping elements for each page
# Code for Map1
if (pgIndex == 1):
#Set inset map position
inset = arcpy.mapping.ListDataFrames(tempMap, "LocatorDF")[0]
inset.ElementPositionX = 3.25
inset.ElementPositionY = 0
# Move text elements
elm = arcpy.mapping.ListLayoutElements(tempMap, "TEXT_ELEMENT", "Westport")[0]
elm.ElementPositionX = 0.3125
elm.ElementPositionY = 3.0227
# Code for Map2
if (pgIndex == 2):
#Set inset map position
inset = arcpy.mapping.ListDataFrames(tempMap, "LocatorDF")[0]
inset.ElementPositionX = 0
inset.ElementPositionY = 3.25
# Move text elements
elm = arcpy.mapping.ListLayoutElements(tempMap, "TEXT_ELEMENT", "Westport")[0]
elm.ElementPositionX = 2.3125
elm.ElementPositionY = 2.0227
# Code for Map3
if (pgIndex == 3):
#Set inset map position
inset = arcpy.mapping.ListDataFrames(tempMap, "LocatorDF")[0]
inset.ElementPositionX = 3.25
inset.ElementPositionY = 3.25
# Move text elements
elm = arcpy.mapping.ListLayoutElements(tempMap, "TEXT_ELEMENT", "Westport")[0]
elm.ElementPositionX = 1.3125
elm.ElementPositionY = 2.0227
# Code to export current page and add it to mapbook
tempDDP.exportToPDF(temp_filename, "RANGE", pgIndex)
finalPdf.appendPages(temp_filename)
arcpy.RefreshActiveView()
# Clean up
del tempMap
# Update the properties of the final pdf
finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",
pdf_layout="SINGLE_PAGE")
# Save your result
finalPdf.saveAndClose()