I can not figure this out. I have a python script that runs as a script in a Toolbox. The script takes features that are selected, performs some calculations and creates two tables. One table is used to generate a report and the other table is used to generate a pie chart. The report has a graphic element at the top that is supposed to reference the jpeg of the pie chart. However when the script tool runs, the pie chart is not created, and thus, a blank space shows up on the report. I took the portion of the script that creates the pie chart and then the report (that portion below) and saved it to another script and ran this directly in the ArcMap python window and it works perfect, so I know that the elements of the scripting are working. I really need this to work through the Toolbox, though. Any suggestions?
This is the script that creates the report.
#Recreate the graph for the report......
#Set local variables....
output_graph_name = "NitrogenAvailability"
output_graph_jpg = "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\GWMA_PieChart.jpg"
graph_grf = "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\NitrogenAvailability.grf"
inputData = "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\Reports\\selections.gdb\\pieTable"
#Create the graph.......
graph = arcpy.Graph()
#Add a pie chart.......
graph.addSeriesPie(inputData, "Nitrogen", "", "NSource")
#Output a graph, which is created in-memory
arcpy.MakeGraph_management(graph_grf, graph, output_graph_name)
#Save the graph as an image....
arcpy.SaveGraph_management(output_graph_name, output_graph_jpg, "IGNORE_ASPECT_RATIO", "500", "300")
#Create a Report of the Nitrogen Table to Summarize the Final Numbers.....
newreport = "nitrogen"
newreportpath = "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\" + newreport + ".pdf"
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd)
table = "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\selections.gdb\\nitrogen"
tabView = arcpy.mapping.TableView("\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\selections.gdb\\nitrogen")
table = arcpy.mapping.ListTableViews(mxd, "nitrogen", df)
arcpy.mapping.ExportReport(table, "\\\\GIS-Cascade\\Data\\Geodatabase\\earth\\GWMA\\Summary\\Reports\\total_nitrogen_summary.rlf", newreportpath)
If the script works then that is good.
If you want to make this into a tool for use in arctoolbox, normally you set parameters for the inputs and outputs to make them useful with other datasets. Your paths are hardcoded at present. Have you examine the documentation on creating script tools? (forget python toolboxes for now)
Do you have your enivronment set to add the output of geoprocessing operations to the map? When you run within a map document, the environment variable for addOutputToMap is probably already true, but may not in your standalone script.
Also, are you trying to just create a new layout element from the graph, or trying to overwrite the path of the old layout element with the old graph to the path of the new graph?
/blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=d57231b1-81e4-474b-9b40-0152b23... for future reference when posting code.
I will definitely check out the output operations in Python. Sorry about the poor code posting --- thanks for the link to posting Python code correctly.
The script is set up to be re-run within the same ArcMap session, so a new pie chart is created (and overwrites the old one) each time the user makes a different selection. The new chart is added to the report that reflects the new data. The end user of this isn't very GIS savvy, they can add layers and make selections, but prefer to have "one button" to hit to run everything.
I have a similar script that runs perfectly, but it is run from a task scheduler and so a completely stand alone python script. The difference has to be in running it from ArcMap.
Thanks for your help.
I figured this out and it was all programmer error. I didn't delete a cursor prior to running this portion of the script that creates the pie chart. Added a del cursor and it works like a charm. Thanks for the help and suggestions.