ArcGIS for Desktop

  • Documentation
  • Pricing
  • Support

  • My Profile
  • Help
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS for Desktop

A complete professional GIS

ArcGIS for Server

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Pricing
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

Help

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • More...

Integrating scripts within a model

  • Example application
  • The Python script
  • Creating a script tool
  • Adding the script tool to 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

  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

  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()
Feedback on this topic?

ArcGIS for Desktop

  • Home
  • Documentation
  • Pricing
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Insiders Blog
  • User Conference
  • Developer Summit
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | Privacy | Legal