I have the below code to export a map series into multiple PNGs. On line 13, I'm getting an error "AttributeError: 'pageRow' object has no attribute 'page_name_field'"
When I run the script, I input the name of the field as a string, in this case "PageNumber." However, when I run this by just putting "pageName = ms.pageRow.PageNumber" it runs correctly. Is there a reason I can't use a variable in this case?
import arcpy
import os
import sys
aprx = arcpy.mp.ArcGISProject("CURRENT")
layout_input = arcpy.GetParameterAsText(0)
resoultion_input = int(arcpy.GetParameterAsText(1))
output_folder = arcpy.GetParameterAsText(2)
page_name_field = arcpy.GetParameterAsText(3)
lyt = aprx.listLayouts(layout_input)[0]
ms = lyt.mapSeries
for pageNum in range(1,ms.pageCount +1):
ms.currentPageNumber = pageNum
pageName = ms.pageRow.page_name_field
arcpy.AddMessage(f"Exporting {pageName}")
lyt.exportToPNG((os.path.join(output_folder, f'Ex2_{pageName}.png')), resolution = resoultion_input)
Solved! Go to Solution.
Because when you are trying to use ms.pageRow.page_name_field, python is looking for an attribute in the pageRow object literally called page_name_field which does not exist. You can use python's getattr() function to dynamically evaluate the variable. E.g.
pageName = getattr(ms.pageRow, page_name_field)
Because when you are trying to use ms.pageRow.page_name_field, python is looking for an attribute in the pageRow object literally called page_name_field which does not exist. You can use python's getattr() function to dynamically evaluate the variable. E.g.
pageName = getattr(ms.pageRow, page_name_field)
Thanks, this worked!