Here is a snippet of code that adds a colgroup to the first table in a html doc
def add_colgroup(html: str):
soup = bs4.BeautifulSoup(html, 'html5lib')
table = soup.table
headers = table.find_all('th')
colgroup = soup.new_tag('colgroup')
for _ in headers:
col = soup.new_tag('col')
col.encode(formatter='html5')
col['style'] = 'text-align: left;width: auto'
colgroup.insert(1, col)
table.insert(1, colgroup)
return soup.prettify()
It creates the following;
<colgroup>
<col style="text-align: left;width: auto"/>
<col style="text-align: left;width: auto"/>
<col style="text-align: left;width: auto"/>
<col style="text-align: left;width: auto"/>
<col style="text-align: left;width: auto"/>
</colgroup>
How do I remove the trailing / ?