Trouble Using a Definition Query Inside Python ExportReport command

1274
1
09-26-2016 01:54 PM
LEAPublisher
New Contributor II

Warning: Novice Python user!!

I found a great script that will iterate through a feature class and create a separate PDF for each feature using the ExportReport function from the Python Window in ArcMap.  It works great if I set the dataset_option to "ALL" but that produces a separate PDF for each feature with all of the reports in each PDF.  The example that I found includes a definition query for the dataset_option, which should only include one report per PDF, but that is the part that I keep getting an error for.  I can't get the report_definition_query to work.

Any suggestions on how to fix the report definition query?  I've been trying to get this to work off an on for a while now, so this would be a great help.  

As a side note, the only reason that I need to do this rather than simply export all of the reports directly from Report Designer is that my reports are rich in images and I can't export more than one report at a time using said Report Designer...

Here is the my code with the query that currently is causing an error (error is listed below code).  

>>> import arcpy
... mxd = arcpy.mapping.MapDocument(r"C:\\Users\\MCrow.SMEINC\\Desktop\\reporttry.mxd")
... 
... df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
... 
... lyr = arcpy.mapping.ListLayers(mxd, "Form_3", df)[0]
... 
... fc = 'C:\\Users\\MCrow.SMEINC\\Downloads\\Lookout_MountainExport\\1929dd550983485a8f0c17a7ac69f007.gdb\\Form_3'
... 
... Fields = ['ManholeID']
... 
... with arcpy.da.SearchCursor(fc, Fields) as cursor:
... for row in cursor:
... 
... manhole = (row[0])
... 
... arcpy.mapping.ExportReport(lyr,
... r"C:\\Users\\MCrow.SMEINC\\Desktop\\ManholeInspections with Pipes.rlf",
... 
... r"C:\\Users\\MCrow.SMEINC\\Desktop\\exportreport\ProjectReport"+str(manhole)+".pdf",
... 
... "DEFINITION_QUERY",
... 
... report_definition_query="ManholeID="+str(manhole)+"")
... 
... del mxd


Here is the error:

Runtime error
Traceback (most recent call last):
File "<string>", line 35, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 532, 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

I am using ArcGIS 10.4.1 and running it on Windows 7 Professional 64 bit.  Python 2.7

Any suggestions would be very much appreciated.

0 Kudos
1 Reply
BrittneyWhite1
Esri Contributor

It looks like your double quotes around the definition query are the issue. Instead of having the whole query surrounded with double quotes ("), you can use triple double quotes (""") to open and close the entire string. This allows you to have double quotes around the field name.  As a personal preference, I prefer the Python string format method to using the + for concatenation.

Incorporating the above, the definition query would look like this:

""" "ManholeID" = {}""".format(manhole)#If manhole = 1, the expression would be ' "ManholeID" = 1'


You also want to check the syntax of the overall expression. One strategy you can use is to build the definition query manually and check to see if the syntax matches to what you are building. 

You may be interested in checking out this article in the documentation: Specifying a query in Python

0 Kudos