Arcpy.mp Map Series- Field Name from code

2426
5
Jump to solution
08-28-2018 07:33 AM
AaronWorthley1
Occasional Contributor II

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")

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StephenM
Occasional Contributor II

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

5 Replies
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
AaronWorthley1
Occasional Contributor II

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It don't think it is possible to concatenate/append the property to the object

0 Kudos
StephenM
Occasional Contributor II

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
AaronWorthley1
Occasional Contributor II

Worked great, Thanks!

0 Kudos