Export Report with Definition Query

2539
7
06-27-2016 08:44 AM
Highlighted
New Contributor

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

Reply
0 Kudos
7 Replies
Highlighted
Regular Contributor III

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

Highlighted
New Contributor

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

Reply
0 Kudos
Highlighted
Regular Contributor III

Could you re-post your corrected code?

Code Formatting... the basics++

Did you check if  "GLUE_ID="+str(play)+"" returns anything?

Highlighted
New Contributor

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.

Reply
0 Kudos
Highlighted
Regular Contributor III

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

Highlighted
New Contributor

Wes - Brilliant!  Works great.  Thanks!!  Can you elaborate on the logic behind needing the sql expression for ExportReport?

Reply
0 Kudos
Highlighted
Regular Contributor III

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.