3

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
5
  • 1
    Could you post a clean copy of your code with any modifications you've made to it? I'm getting confused by what you have and have not changed. Thanks, Apr 26, 2013 at 1:10
  • @GetSpatial you're right, it was getting messy. Added it at the top, will go back and delete error messages etc. later once it gets worked out.
    – David Meek
    Apr 26, 2013 at 1:16
  • Now, from looking at the code, there seem to be two different operations happening. One is creating a report from this report template that you have. The other is exporting pdf's of a set of data driven pages. It would help if you separated the variables and code related to each of these tasks into their own blocks of code. This way it is easier, both for you and for others reading it, to see what variables and functions go together. Apr 26, 2013 at 1:29
  • 1
    Made a couple edits to my answer that you might look at when you have a chance. Apr 26, 2013 at 2:19
  • Did another edit, see if it works. If not, maybe time to explore other avenues. If anyone else on GISse has ideas, throw them out there. Apr 26, 2013 at 9:35

1 Answer 1

3

There are a couple places you might be running into trouble.

One place is for your argument for Page Range.

The help file: ExportReport, states that this page_range argument needs to be a string. It needs to be enclosed in quotes.

Change your entry from 1-5 to "1-5", and see if that makes a difference.

The second place may be related to the Extent. Since you are choosing Definition_Query for the Dataset Option, you cannot include the Extent parameter. I think having an empty string as a placeholder is causing a problem.

Try this, instead, as borrowed from the examples on the help page:

arcpy.mapping.ExportReport(graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, "1-5",where_clause,field_map=field_map)

Instead of having to enter something for every parameter, they allow you to specify a particular parameter using the syntax parametername=value.

Edit:
Take a look at your Field Mappings. According to the help document, the mapping should be in the order of Field name from template:Field name from dataset

Here is what you have currently:

field_map ={"Incident_Z":"zipcode", "Incident_A":"Address", "Precinct": "pp"}

Based on your code on previous questions, I am pretty sure that the "Incident_Z" field is in your dataset, since that is part of what you are using for the definition query. If that is the case, the names should probably be switched, like so:

field_map ={"zipcode":"Incident_Z", "Address":"Incident_A", "pp":"Precinct"}

See if that makes any changes.

Also, while you are at it, I think it would be a good idea to more directly specify the input layer.

This syntax leaves it kind of ambiguous, especially if some layers were switched:

graffitilyr=arcpy.mapping.ListLayers(mxd, "*", df)[0]

This will simply return the first layer in the table of contents. Since having the correct input layer is critical to the report working, it would be better to narrow down the list to guarantee that you are getting the right layer. You do this by adding keywords instead of the "*", in this case, "Graffiti" could work, like so:

graffitilyr=arcpy.mapping.ListLayers(mxd, "Graffiti", df)[0]

This will return the first layer that has "Graffiti" in the name, as opposed to the first layer in the table of contents. It gives you the flexibility of placing the layer in whatever position works best for you in the Table of Contents.

Edit 2:

First, I went through your script and found one syntax error that may be causing a problem. It is where you declared the file path for New_Layer. You are missing one backslash after the C:\ drive.
See highlight in picture:
File path with incorrect backslash escape

Whether or not the missing backslash is the problem, we need to simplify the testing process. That means getting rid of code and testing components. I stripped out all of the code that does not pertain to the report creation. I added a couple of Print functions to print out the layer name and the definition query so that you can check if it is correct.

Since there seems to be a file access error with the report template, I wanted to mention a potential discrepancy with regard to file paths. You are pulling from two different locations. Your mxd and .rlf are on the "C:\" drive. Your output .pdf is being saved to the "Z:\" drive. It is not clear where the source dataset is saved since it is only referenced through the .mxd. Make sure that these file paths are correct, and that you have read/write access to each location.

Included below, is what is left. I have included a function that I mentioned in a different answer to have arcpy insert the appropriate Field Delimiters

import arcpy

arcpy.env.overwriteOutput =True

#Set Source MXD
mxd = arcpy.mapping.MapDocument("C:\\DM_Final_submission\\Graffiti_DM3_5.mxd")

#Set Source Dataframe
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

#Set Source dataset for report
graffitilyr=arcpy.mapping.ListLayers(mxd, "Graffiti", df)[0]

#Test Layer Retrieval
print (graffitilyr.name)

#Create Definition query of field name and zip code
queryfield = "Incident_Z"
where_clause = arcpy.AddFieldDelimiters(graffitilyr,queryfield) + " = 10002"

#Test
print (where_clause)

#Create new field mappings for report definition
fieldmap ={'zipcode':'Incident_Z', 'Address':'Incident_A', 'pp':'Precinct'}
#Define export directory
expdir="Z:\\DM_Final_submission"

#Define path to report template
rlf= "C:\\DM_Final_submission\\2013_Graffiti.rlf"

#Define export full path to export file
export= expdir + "\\" + "repexpnamepdf.pdf"

#Set up Export Report function with all parameters
arcpy.mapping.ExportReport(graffitilyr, rlf, export,"DEFINITION_QUERY","New Graffiti Report", 1, "1-5",where_clause,field_map=fieldmap)

I would say that at this point, if it doesn't work, try to do the exact same report export from within the MXD. Use the same files, the same field mappings, the same template, etc. and see if it works. Also consider opening up a support case with ESRI.

4
  • Unfortunately, while a very good couple suggestions, none worked. What's really strange though about it not working is the error message; I'm editing the original post to show it and explain as it won't fit here.
    – David Meek
    Apr 26, 2013 at 0:42
  • unfortunately, the problem persists despite making the "Graffiti" change and the reversal of the field mapping. Still the error re: inability to open report template (also, I verified ArcMap was closed).
    – David Meek
    Apr 26, 2013 at 2:46
  • Many thanks for the excellent suggestions. Unfortunately, still no luck. It's conceivable it has to do with the read/write issue. I'm on a Mac running Parallels, and for some reason am having trouble from time to time writing to the PC-side "C" drive, and so am storing and running stuff from there, but saving to Mac "Z". What doesn't make sense about this being the problem is that I've written 3 other variations on this script, all of which work, all of which with the same save to Z part. Will keep plugging away at it. Thanks again.
    – David Meek
    Apr 26, 2013 at 11:05
  • 1
    Based on this comment, I would try putting your data paths either all to C:` or all to Z:` and see if that works. Sometimes even if you are just reading a file, you need write access to the directory as well because a lock is placed on the file, modifying it in some way. This could be the case with the .rlf file. Apr 26, 2013 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.