I'm trying to iterate over the rows of an html Table and appending a new td tag each time to create a second column. The Problem is, that the .select('tr:nth-of-type(INDEX))'
method does not seem to work like I thought it would, because the output is unmodified. Are there classes in bs4 that create indexable containers from htmls in bs4? Is there another module that can do that?
Here's an excerpt from my html table with 3 rows:
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
mso-yfti-tbllook:1184;mso-padding-alt:0cm 5.4pt 0cm 5.4pt">
<tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;height:11.45pt">
<td style="width:163.55pt;border-top:solid windowtext 1.0pt;
border-left:none;border-bottom:solid windowtext 1.0pt;border-right:none;
mso-border-top-alt:solid windowtext .5pt;mso-border-bottom-alt:solid windowtext .5pt;
padding:0cm 5.4pt 0cm 5.4pt;height:11.45pt" width="218">
<p align="center" class="MsoNormal" style="margin-bottom:0cm;margin-bottom:.0001pt;
text-align:center;line-height:normal">
<span style='font-size:10.0pt;
font-family:"CMU Serif"'>
Compound
<o:p>
</o:p>
</span>
</p>
</td>
</tr>
<tr style="mso-yfti-irow:1;height:11.45pt">
<td style="width:163.55pt;border:none;padding:0cm 5.4pt 0cm 5.4pt;
height:11.45pt" width="218">
<p align="center" class="MsoNormal" style="margin-bottom:0cm;margin-bottom:.0001pt;
text-align:center;line-height:normal">
<span style='font-size:10.0pt;
font-family:"CMU Serif"'>
Empirical formula
<o:p>
</o:p>
</span>
</p>
</td>
</tr>
<tr style="mso-yfti-irow:2;height:11.45pt">
<td style="width:163.55pt;border:none;padding:0cm 5.4pt 0cm 5.4pt;
height:11.45pt" width="218">
<p align="center" class="MsoNormal" style="margin-bottom:0cm;margin-bottom:.0001pt;
text-align:center;line-height:normal">
<span style='font-size:10.0pt;
font-family:"CMU Serif"'>
Formula weight / g
</span>
<span style='font-size:
10.0pt;font-family:"Cambria Math",serif;mso-bidi-font-family:"Cambria Math"'>
⋅
</span>
<span style='font-size:10.0pt;font-family:"CMU Serif"'>
mol
<sup>
-1
<o:p>
</o:p>
</sup>
</span>
</p>
</td>
</tr>
</table>
And here's my code:
def template_file_opener(path):
'''Opens the template.htm and returns it as a bs4 Object'''
with open(path,'r') as inf:
soup = BeautifulSoup(inf,features='html.parser')
return soup
template = template_file_opener('template.htm')
for i,tr in enumerate(template.find_all('tr')):
for j,td in enumerate(tr.find_all('td')):
temporary_td = str(td)
old_text = td.text.strip()
new_text = str(100000000000000000000000000000000000 + i*1000000000)
new_td = temporary_td.replace(old_text,new_text)
template.table.select('tr:nth-of-type({})'.format(i)).append(BeautifulSoup(new_td, 'html.parser'))
res = template.table.prettify()
print(res)
What am I doing wrong?