Trying to export individual report for each row in a layer. Seems the definition query piece is not working. Any ideas? Code is :
import arcpy, os
path = os.getcwd()
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "BOB.TEST")[0]
fields = ['GLUE_ID']
with arcpy.da.SearchCursor(lyr, Fields) as cursor:
for row in cursor:
play = (row[0])
arcpy.mapping.ExportReport(lyr,
r"C:\\Bob\\ExecutiveSummary.rlf",
r"C:\\Bob"+str(play)+".pdf",
"DEFINITION_QUERY",
report_definition_query="GLUE_ID="+str(play)+"")
del mxd
You used to different spellings of fields,Fields. Does your definition query work if used in a regular query? You should be using AddFieldDelimiters—Help | ArcGIS for Desktop in case you are mixing geodatabases and shapefiles
Wes - was just a typo on fields/Fields. Changed the field delimiters - thanks! Still getting problem with arcpy.mapping.ExportReport line. Here's error:
Runtime error
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 515, in ExportReport
return report_source._arc_object.ExportReport(*gp_fixargs((report_layout_file, output_file, dataset_option, report_title, starting_page_number, page_range, report_definition_query, extent, field_map), True))
RuntimeError: Error in generating report
Could you re-post your corrected code?
Code Formatting... the basics++
Did you check if "GLUE_ID="+str(play)+"" returns anything?
import arcpy, os
path = os.getcwd()
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "UNV.TEST")[0]
fields = "GLUE_ID"
with arcpy.da.SearchCursor(lyr, fields) as cursor:
for row in cursor:
play = (row[0])
arcpy.mapping.ExportReport(lyr, r"C:\\Bob\\ExecutiveSummary.rlf", r"C:\\Bob"+str(play)+".pdf", "DEFINITION_QUERY", report_definition_query="GLUE_ID="+str(play))
del mxd
Yes the query alone returns the values of GLUE_ID.
See if this will work
import arcpy, os path = os.getcwd() mxd = arcpy.mapping.MapDocument("CURRENT") lyr = arcpy.mapping.ListLayers(mxd, "UNV.TEST")[0] fields = "GLUE_ID" with arcpy.da.SearchCursor(lyr, fields) as cursor: for row in cursor: play = (row[0]) sql_exp = """{0} = '{1}'""".format( arcpy.AddFieldDelimiters(lyr, fields), play) arcpy.mapping.ExportReport(lyr, r"C:\\Bob\\ExecutiveSummary.rlf", r"C:\\Bob"+str(play)+".pdf", "DEFINITION_QUERY", report_definition_query = sql_exp) del mxd
Wes - Brilliant! Works great. Thanks!! Can you elaborate on the logic behind needing the sql expression for ExportReport?
The logic is simply formatting the query correctly to make the query work. using the AddFieldDelimiters ensures the field names gets the proper characters around them.whether it is a feature class from a file, personal, arcgis server geodatabase or a shapefile.