Ah, nevermind; I believe I've found a solution. The problem seems to be with BeautifulSoup itself. It seems to be getting a bit confused.
The solution is to force the BeautifulSoup to be strings with "str":
output = "<table>"
output += "<tr>\n"
output += "<td>" + str(name) + "</td>"
output += "<td>" + str(companyprofile) + "</td>"
output += "<td>" + str(contacttable) + "</td>"
output += "<td>" + str(z) + "</td>"
output += "<td>" + str(keypeople) + "</td>"
output += "</tr>"
output += "</table>"
return output