In formatImages() in cutils.py, you're souping a single line of xhtml that has an invalid closing div tag at the end of it. What did you think the parser would do with the extraneous closing tag?
Code:
soup = BeautifulSoup(line, 'html.parser')
where "line" is:
Code:
<img alt="" class="img065" src="../Images/lsh-23-065.png"/></div>
Will definitely drop the closing div.
This code:
commits the divless soup back to the xhtml.
You need to find a better way of isolating the img tag and writing the modified one(s) back without affecting the surrounding code.
The attached python script should make what's happening clear.