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,, 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 script. For more details about creating script tools, see A quick tour of creating script tools

  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
    Setting script tool parameter properties
  5. Click Finish to add the script tool to the toolbox.

Adding the script tool to a model

  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 sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)

tablePath = gp.GetParameterAsText(0)
filePath = gp.GetParameterAsText(1)

outfile = open(filePath, "w")
fields = gp.ListFields(tablePath)

fieldNames = []
for field in fields:
   if (field.type <> "Geometry" and field.type <> "BLOB"):
outfile.write("<table border=""1"">\n")

for fieldName in fieldNames:
    outfile.write("<th>" + fieldName + "</th>\n")

cur = gp.SearchCursor(tablePath)
row = cur.Next()
while row:
    for fieldName in fieldNames:
        outfile.write("<td>" + str(row.getValue(fieldName)) + "</td>\n")

    row = cur.Next()

del cur