I need to find a way on calculating the position (that means the index of the character position) of text located in a div-element into the according position of HTML-Code. This is necessary because I must be able to insert an element at the position without losing the formatting.
For example I have the following:
HTML
<p>
Lorem<strong>
ipsum</strong>
dolor sit...</p>
which is interpreted to:
Text
Lorem ipsum dolor sit...
Now I would like to insert a string-element at a specific position inside the Text:
Lorem ipsum d
<insertion>
olor sit...
This is at string-index: 13
Regarding to that the position of the insertion should be 32
in my HTML, because there are HTML-Tags: <p>
, <strong>
, </strong>
which must be also counted to find the correct position inside the HTML.
I only have those informations:
- text as string (that means without any tags)
- text as HTML
- index of text-string where the insertion has to be placed (it's the
13
in my example)
The solution should be in Python
. I played around with the BeautifulSoap
module, but didn't find a way to insert text at a specific index inside an element.
Hope someone can help me with this. Many thanks in advance!