Do you mean you want to format your output automatically in Python, i.e. wrapping it in appropriate HTML tags?
Or just do you mean save whatever comes out of your program into a file with a .html extension?
For appending to a file, just open it with append mode, i.e. filehandle = open("myfile.html", "a") Your question was way too vague. I can't read your mind, nor magically know what the output coming out of your program is like. You need to provide details.
Then you'll want to take each line of the output and search it. If it finds "FAILED" in the string, wrap the red-colour style tag around that line; if it finds "PASSED", wrap the green-coloured tag around it. Something like:
# assuming 'output' is a list of the output lines
for index, value in enumerate(output):
if "PASSED" is in value:
output[index] = "<span class='green'>" + value + "</span>"
output[index] = "<span class='red'>" + value + "</span>"
Finally, save it to a file opened so that its saved with a .html extension. You'll need to include the HTML structure (<html><head><body>, etc.) into the string you save though:
This is very basic of course, and if you're wanting to update the records by just appending new ones, I assume you'll need to open up the HTML, remove the ending body and html tags, add the records, and then reattach those tags.