#!/usr/bin/python
import sys,dbflib

if len(sys.argv) != 2:
    print "Needs a filename"
    sys.exit(2)

dbf = dbflib.open(sys.argv[1],"rb")

print """<html>
<head>
<title>Display %s as HTML table</title>
</head>
<body>""" % sys.argv[1]
print '<table border="1">'
tablehead = "<tr>"
for i in range(dbf.field_count()):
    type, name, len, decc = dbf.field_info(i)
    tablehead = tablehead + "<th>%s</th>" % name
tablehead = tablehead + "</tr>"

print tablehead
format = "<tr>"
for i in range(dbf.field_count()):
    type, name, len, decc = dbf.field_info(i)
    if type == 0:
	format = format + "<td>%%(%s)s</td>" % name
    elif type == 1:
	format = format + "<td>%%(%s)d</td>" % name
    elif type == 2:
	format = format + "<td>%%(%s)g</td>" % name
format = format + "</tr>"
#print format
for i in range(dbf.record_count()):
    print format % dbf.read_record(i)

print "</table>"
print """</body>
</html>"""
