I'm trying to use the arcpy MapSeries Class to generate PNGs for each sheet, based on Code Example #2 here: http://pro.arcgis.com/en/pro-app/arcpy/mapping/mapseries-class.htm#C_GUID-5F34FFE0-FC62-481E-B960-A7....
I'd like to get the Index field name from the Map Series Index Layer to make the code flexible rather than having to hard-code in the field name in the pageRow property.
My attempts don't work, although it's easy enough to find the I can't figure out how to pass a variable into the FIELD_NAME portion of the pageRow property.
using either pageField = ms.pageNameField.name
or pageField = str(ms.pageNameField.name)
fails on: pageName = ms.pageRow.pageField
with: AttributeError: 'pageRow' object has no attribute 'pageField'
Is there any way to insert a variable into the Field Name attribute? layout.MapSeries.pageRow.FIELD_NAME
import arcpy
#get parameters
SaveLocation = arcpy.GetParameterAsText(0)
aprx = arcpy.mp.ArcGISProject("CURRENT")
l = aprx.listLayouts()[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
for pageNum in range(1, ms.pageCount + 1):
ms.currentPageNumber = pageNum
pageField = ms.pageNameField.name
pageName = ms.pageRow.pageField
l.exportToPNG(SaveLocation + "\\" + pageName + ".png")
Solved! Go to Solution.
I had this same question and got it to work using the getattr() function.
if ms.enabled:
name_field = ms.pageNameField.name
for pageNum in range(1, ms.pageCount + 1):
ms.currentPageNumber = pageNum
pageName = getattr(ms.pageRow, name_field)
MapSeries—ArcPy | ArcGIS Desktop
In their examples they hardcode the page name
pageName = ms.pageRow.STATE_NAME
Unfortunately they give no indication as to whether the actual example's field name is in all caps or not.
They have an example of using a pagenumber.
There is also a pagename field property and
getPageNumberFromName (page_name) method that might make it easier
Right, and I have no problem when I hard-code in my own field name
ie. pageName = ms.pageRow.MyFieldName
but I want to substitute MyFieldName with the active Field Name from the code so I don't have to change it every time....I can easily get that field name using this: ms.pageNameField.name
but can't figure out how to get it into the Field Name part of the pageRow property.
It don't think it is possible to concatenate/append the property to the object
I had this same question and got it to work using the getattr() function.
if ms.enabled:
name_field = ms.pageNameField.name
for pageNum in range(1, ms.pageCount + 1):
ms.currentPageNumber = pageNum
pageName = getattr(ms.pageRow, name_field)
Worked great, Thanks!