I'm running into what I believe is a parameter error in writing an arcpy exporttoreport script. I've written several successful scripts of this same type, but the difference is that this one has a "DEFINITION_QUERY", which requires a query. Because of the order of parameters, I need to have something between the where_clause and field map, but using "" doesn't seem to be working. Below are the script and error messages. For the second error message I tried something I saw on the help page of including a double bracket and then ...",True) at the very end, that prompted an error about 3 versus 2 arguments. Any suggestions would be much appreciated!
Current and clean version of code
import arcpy
arcpy.env.overwriteOutput =True
mxd = arcpy.mapping.MapDocument("C:\\DM_Final_submission\\Graffiti_DM3_5.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
New_layer= "C:\DM_Final_submission\\New_Graffiti occurrence.lyr"
zip_code='"Incident_Z" = ' + "'10002'"
where_clause='"Incident_Z" = ' + "'10002'"
field_map ={"Incident_Z":"zipcode", "Incident_A":"Address", "Precinct": "pp"}
finalpdf="final.pdf"
graffitilyr=arcpy.mapping.ListLayers(mxd, "*", df)[0]
expdir="Z:\\DM_Final_submission"
rlf= "C:\\DM_Final_submission\\2013_Graffiti.rlf"
export= expdir + "\\" + "repexpnamepdf.pdf"
newpointout="Z:\\DM_Final_submission\\newpointout1.shp"
finalpdf="Z:\\DM_Final_submission\\" + finalpdf
newpointlayer="Z:\\DM_Final_submission\\newpointlayer.lyr"
pdfddp="Z:\\DM_Final_submission\\pdfddp2.pdf"
ddp = mxd.dataDrivenPages
nycstreets="C:\\DM_Final_submission\\NYCStreets.lyr"
newnycstreets="Z:\\DM_Final_submission\\newstreetsout.shp"
this code block selects a series of points and outputs them as a new shapefile based upon a query (zip_code)
arcpy.Select_analysis(New_layer, newpointout, zip_code )
this code block selects a series of streets and outputs it as a new shapefile based upon a query (zip_code)
arcpy.Select_analysis(nycstreets, newnycstreets, zip_code)
this code block takes the previously created new point .shp file and and outputs it as a new layer
arcpy.MakeFeatureLayer_management(newpointout, newpointlayer)
this code block creates the report (graffitilyr is the input, rlf is the .rlf, export is the export path, "DEFINITION_QUERY" is the parameter for data_set_option, New Graffiti Report" is the new title, 1 is the starting page, 1-5 is the range, where_clause is a query for definition query, field map is field map)
arcpy.mapping.ExportReport(graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, "1-5",where_clause,field_map=field_map)
the following code does DDP, creates a blank pdf, and then appends a variety of pdfs, including the report to it.
ddp.exportToPDF(pdfddp, "ALL")
mapbookPDF= arcpy.mapping.PDFDocumentCreate(finalpdf)
mapbookPDF.appendPages (expdir + "\\overviewmap.pdf")
mapbookPDF.appendPages(expdir + "\\repexpnamepdf.pdf")
mapbookPDF.appendPages(expdir + "\\pdfddp2.pdf")
mapbookPDF.saveAndClose()
Edited, and edits at bottom
arcpy
arcpy.env.overwriteOutput =True
mxd = arcpy.mapping.MapDocument("C:\\DM_Final_submission\\Graffiti_DM3_5.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
New_layer= "C:\DM_Final_submission\\New_Graffiti occurrence.lyr"
zip_code='"Incident_Z" = ' + "'10002'"
where_clause='"Incident_Z" = ' + "'10002'"
field_map ={"Incident_Z":"zipcode", "Incident_A":"Address", "Precinct": "pp"}
finalpdf="final.pdf"
graffitilyr=arcpy.mapping.ListLayers(mxd, "*", df)[0]
expdir="Z:\\DM_Final_submission"
rlf= "C:\\DM_Final_submission\\2013_Graffiti.rlf"
export= expdir + "\\" + "repexpnamepdf.pdf"
newpointout="Z:\\DM_Final_submission\\newpointout1.shp"
finalpdf="Z:\\DM_Final_submission\\" + finalpdf
newpointlayer="Z:\\DM_Final_submission\\newpointlayer.lyr"
pdfddp="Z:\\DM_Final_submission\\pdfddp2.pdf"
ddp = mxd.dataDrivenPages
nycstreets="C:\\DM_Final_submission\\NYCStreets.lyr"
newnycstreets="Z:\\DM_Final_submission\\newstreetsout.shp"
arcpy.Select_analysis(New_layer, newpointout, zip_code )
#arcpy.Select_analysis(nyczip, newzipcode, specifyzipzip)
arcpy.Select_analysis(nycstreets, newnycstreets, zip_code)
arcpy.MakeFeatureLayer_management(newpointout, newpointlayer)
arcpy.mapping.ExportReport (graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, 1-5,where_clause, " ", field_map)
ddp.exportToPDF(pdfddp, "ALL")
mapbookPDF= arcpy.mapping.PDFDocumentCreate(finalpdf)
mapbookPDF.appendPages (expdir + "\\overviewmap.pdf")
mapbookPDF.appendPages(expdir + "\\repexpnamepdf.pdf")
mapbookPDF.appendPages(expdir + "\\pdfddp2.pdf")
mapbookPDF.saveAndClose()
Traceback (most recent call last):
File "C:\\New_file_New_query1.py", line 30, in <module>
arcpy.mapping.ExportReport (graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, 1-5,where_clause, " ", field_map)
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 514, 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))
ValueError: Can't set these parameters with selected dataset_option
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\\New_file_New_query1.py", line 30, in <module>
arcpy.mapping.ExportReport ((graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, 1-5,where_clause, " ", field_map), True)
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
TypeError: ExportReport() takes at least 3 arguments (2 given)
>>> ================================ RESTART ================================
Based upon the suggestion below of @GetSpatial, I edited the code to read
arcpy.mapping.ExportReport(graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, "1-5",where_clause,field_map=field_map)
However that produced the following error. In reviewing the traceback, what is interesting is A) that it says the error is that it can't open the report layout template (note I checked in ArcMap and the .rlf opens fine, I also reran another version of the script that uses the .rlf and it ran correctly), and particularly interesting is B) something I hadn't previously seen in the traceback above is that it has a different order for the parameters than that of the help page:
(*gp_fixargs((**report_layout_file**, output_file, dataset_option, report_title, starting_page_number
Specifically, the report source file, which should be first isn't there, but it starts with the .rlf. Could this be the cause (note have also tried taking out the input and replicating that order above from the error, but that doesn't work either)
Traceback (most recent call last):
File "C:\DM_Final_submission\New_file_New_query1.py", line 30, in <module>
arcpy.mapping.ExportReport(graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, "1-5",where_clause,field_map=field_map)
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 514, 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))
IOError: Could not open report template