Friday, September 25, 2009

Table to HTML table

Sometimes it's necessary to generate an html table for returned results. This tool basically takes any format that can support a search cursor, and generates an HTML file on your local disk. I was using this script in conjunction with some reporting tools to analyze AGS server logs.

Enjoy, and I hope it sparks some great GP ideas.



# ---------------------------------------------------------------------------
# TableToHtml.py
# ---------------------------------------------------------------------------

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"):
fieldNames.append(field.name)

outfile.write(" <table border=""1"">\n")

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

outfile.write("</tr>\n")

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

outfile.write("</tr>\n")

row = cur.Next()

del cur

outfile.write("</tbody></table>\n")

outfile.flush()
outfile.close()