Python or other language scripts can be integrated into models by making the script into a script tool and adding the script tool to a model. This can be used when Python or other scripting language logic is needed to enhance a model or to access an external package or program from within ArcGIS.
The starting place for integrating scripts within models is the actual script. The script must be written in such a way that it can be integrated into the geoprocessing framework. For more information, refer to Writing a Python script.
Once the script is written, a script tool can be created and added to a toolbox. This script tool can be added to a model and used to bring additional functionality to the model.
Example application
There is no built-in way to work with HTML in ArcGIS. However, Python scripting provides access to methods and functions that can be used to create and modify HTML documents. To integrate this HTML functionality with ArcGIS, embed a Python script tool into a model.
The following example performs spatial and attribute queries on a parcels layer and generates an HTML report detailing the attributes of a user-specified parcel and neighboring parcels. The HTML generation is performed within a Python script that is executed from the model.
The Python script
The following script, tabletohtml.py, is used to read the contents of an input table and generate an HTML report. The script code is provided at the end of this topic.
Creating a script tool
The steps below show you how to create a script tool that executes the tabletohtml.py script. For more details about creating script tools, see A quick tour of creating script tools
- Right-click a toolbox and click Add > Script.
This opens the Add Script wizard.
- Use the Add Script wizard to specify general script properties.
- At the next page of the Add Script wizard, specify what script will be executed when the script tool is run.
- At the next page of the Add Script wizard, specify the script tool parameter properties. This script tool has two parameters: an input table and an output HTML file. These parameters were also defined in tabletohtml.py.
- Click Finish to add the script tool to the toolbox.
Adding the script tool to a model
- The model Parcel Report performs spatial and attribute queries on a parcels layer. Add the Table to HTML script tool to the model to add the desired HTML generation functionality.
- Connect the output of the Select Layer By Location tool as the input to the script tool and set the path of the script tool output (the path of the HTML file to generate). Additionally, rename the output of the script tool as Report and make the variable a model parameter.
import arcpy
import sys
import string
import os
tablePath = arcpy.GetParameterAsText(0)
filePath = arcpy.GetParameterAsText(1)
outfile = open(filePath, "w")
fields = arcpy.ListFields(tablePath)
fieldNames = []
for field in fields:
if (field.type <> "Geometry" and field.type <> "BLOB"):
fieldNames.append(field.name)
outfile.write("<table border=""1"">\n")
outfile.write("<tr>\n")
for fieldName in fieldNames:
outfile.write("<th>" + fieldName + "</th>\n")
outfile.write("</tr>\n")
for row in arcpy.da.SearchCursor(tablePath, fieldNames):
outfile.write("<tr>\n")
for value in row:
outfile.write("<td>" + str(value) + "</td>\n")
outfile.write("</tr>\n")
outfile.write("</table>\n")
outfile.flush()
outfile.close()