Integrating scripts within a model

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.

Table to html python script

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

Steps:
  1. Right-click a toolbox and click Add > Script.
    Adding a new script

    This opens the Add Script wizard.

  2. Use the Add Script wizard to specify general script properties.
    Script general properties
  3. At the next page of the Add Script wizard, specify what script will be executed when the script tool is run.
    Script source file
  4. 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.
    Setting script tool parameter properties
  5. Click Finish to add the script tool to the toolbox.

Adding the script tool to a model

Steps:
  1. 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.
    Adding script tool to a model
  2. 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.
Example model using script tool
The finished model, which contains a script tool used to generate an HTML report
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()
3/3/2014