There are two errors in your format string. The first, as U9-Forward pointed out, is here:
</html> % (titles[0], titles[1], titles[2], titles[3], titles[4])"""
The %
is an interpolation operator so it needs to go between the string and the data:
</html>""" % (titles[0], titles[1], titles[2], titles[3], titles[4])
The second error, apparent only after you fixed that one, is here:
<table width="100%" height="100%" border="5px">
When you use the %
operator, the character %
becomes special, so that %s
does what you expect. But when that happens, "100%"
isn't legal, because, as the error message told you, it puts an unsupported format character '"' (0x22) at index 237
. You could have found this out for yourself in under a minute by putting your cursor at the beginning of the string and pressing right-arrow 237 times.
In this case, the %
that you want to stay %
must be doubled:
<table width="100%%" height="100%%" border="5px">
That gives
html_text = '''<html>
<head>
<style type="text/css">
table { border-collapse: collapse;}
td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }
</style>
</head>
<body>
<table width="100%%" height="100%%" border="5px">
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
</table>
</body>
</html>''' % (titles[0], titles[1], titles[2], titles[3], titles[4])
But the fundamental problem here is that Python %
-strings are a formatting mini-language, and HTML is a formatting language, and so constructing HTML like this means you are programming in two languages simultaneously. The doublethink that this involves gives some experienced programmers a kick, but the rest of us are happier separating our concerns and dealing with one language at a time. Instead of %
-strings, consider using lxml
to construct your HTML. There is more of a learning curve (eased by an excellent tutorial) but your code will be easier to write and maintain, and lxml
will ensure your HTML is free of syntax errors.