Select to view content in your preferred language

Why can't I use a variable to specify a page name field?

219
2
Jump to solution
11-08-2024 04:44 PM
Labels (1)
rescobar
Regular Contributor

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)

 

 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

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)

 

View solution in original post

2 Replies
Luke_Pinner
MVP Regular Contributor

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)

 

rescobar
Regular Contributor

Thanks, this worked!

 

0 Kudos