# Importing Modules
import os,arcpy,sys,traceback,time,shutil
from datetime import datetime
from arcpy import env
### Establish Local Variables For Exporting Reports
print "*****************************************"
print "Welcome to the Agent RFI Report tool."
print "Establishing local variables..."
arcpy.AddMessage("Establishing Variables")
print ""
try:
agentNO = arcpy.GetParameterAsText(0)
reportFolderDated = "C:/arcgisserver/gisData/projects/RFIs/results/"
reportFolder = "C:/inetpub/wwwroot/flexviewers/_supportDocs/reports/"
pngPath = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.png"
rlfPath = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.rlf"
pdfReport = reportFolder+"RFIreport.pdf"
pdfDatedReport = reportFolderDated+"Agent"+agentNO+"_{}.pdf".format(datetime.strftime(datetime.now(),"%Y-%m-%d_%H%M"))
print "Variables Established"
arcpy.env.overwriteOutput = True
### Establish Local Variables for Reports
mxdReportMap = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.mxd"
mxd = arcpy.mapping.MapDocument(mxdReportMap)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Nexus DB", df)[0]
### Generate report
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfDatedReport)
whereClause = "AGENTNUMBER = "+str(agentNO)+" AND LAT_DEC > "+str(0)
print "Query: " + whereClause
lyr.definitionQuery = whereClause
print "Features Selected"
df.extent = lyr.getSelectedExtent()
arcpy.mapping.ExportToPNG(mxd,pngPath,resolution=200)
print "PNG exported."
arcpy.AddMessage("PNG Exported")
arcpy.mapping.ExportReport(report_source=lyr,report_layout_file=rlfPath,output_file=pdfDatedReport)
pdfDoc.saveAndClose()
if os.path.exists(pdfReport):
os.remove(pdfReport)
print "check pdf"
print "PDF Report Compiled."
arcpy.AddMessage("PDF Report Compiled")
print "Validate Report."
shutil.copy(pdfDatedReport,pdfReport)
## arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION")
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "Oops, you have PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n" \
+ str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "Oops, you have ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
arcpy.AddMessage(msgs)
arcpy.AddMessage(pymsg)
arcpy.AddMessage(arcpy.GetMessages(1))Hi George,
You've probably already discovered this as this post is pretty old now, but ExportReport will not function as a geoprocessing service and is the likely cause of your gp service failure (it is for me running 10.3). Have you found a workaround for this?
Jeff