My question as below:

1. How to save result text to HTML in Python?

2. If i want continue append more result text in the same HTML file, how?

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.

Sorry if my question not clear, i want the result like this:

file.html


EXAM CASES

[2009'07'24 @ 10'56'07] : FAILED : Case1
[2009'07'24 @ 10'56'49] : PASSED : Case2

For FAILED, it will highlighted RED, then PASSED will highlighted by GREEN, how to do it?

I assume you know at least the very basic styling available with CSS. You'll want to make something basic, for a red and a green class in CSS like this:

<style type='text/css'>
    .red { color: #f00; }
    .green { color: #0f0; }
</style>

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>"
    else:
        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:

toSave = """
<html>
<head>
    # insert your CSS styling write-up here
</head>
<body>
""" + my_output_as_a_string + """
</body>
</html>"""

fh = open("file.html", "w")
fh.write(toSave)
fh.close()

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.