Select to view content in your preferred language

Python Table to HTML

2104
2
08-12-2010 01:53 PM
DavidAshton
Frequent Contributor
Basically I have a small python script that is converting one table to a HTML file...Very nice and cleanly.  But I want to convert multiple table to the same HTML file and maybe have multiple tables with the HTML.  Can someone help me with this or point me in the right direction.
Here's the code I have (I didn't write this code, a friend pointed me to it, it was posted in another post)
Thanks
David

 # ---------------------------------------------------------------------------
# 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("<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("</table>\n")

outfile.flush()
outfile.close()

0 Kudos
2 Replies
AndrewChapkowski
Esri Regular Contributor
I would get a list of tables, create a loop that iterates the list like this:

for table in tables:
     # Create table search cursor
     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



Hope this helps
0 Kudos
DavidAshton
Frequent Contributor
Thanks Andrew,

I will give your code a shot.  I was hoping to ask you one more easier question.  I want to the users to be able to create a title for my HTML but I'm having trouble setting it up.  I thought it would be as easy as:

htmlTitle = gp.GetParameterAsText(0)

outfile.write(htmlTitle)

Then when I add the script to a model or a toolbox and run from ArcMap
I right click the script > go to Parameters tab > and set the DataType to string (for the htmlTitle parameter) and I set type to required, Direction to Input, Multi to No, Filter to none, all the rest I leave blank.

In arcmap everything is fine and the user has a text box to enter the name of title, but in server I don't see that text box.  So how do I make that text box appear in Server.

Thanks
David
0 Kudos