'python-docx: how to add pictures to <w:t>

Here's the table I want to generate: enter image description here

I can add picture to the end of the cell, by using the following code:

run = document.tables[7].rows[5].cells[1].paragraphs[0].add_run('text.')
run.add_picture("test.jpg")

You can guess that the cell content is from a markdown editor, I want the pictures to show at the right place, not always to the end of the paragraph of the cell.

Problem is, document.tables[7].rows[5].cells[1] only contains 1 paragraph, here's the xml:

<w:p w14:paraId="273B3AD3" w14:textId="06955DB3" w:rsidR="00882F95" w:rsidRDefault="00366B53">
<w:pPr>
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体"/>
<w:szCs w:val="21"/>
</w:rPr>
</w:pPr>
<w:proofErr w:type="gramStart"/>
<w:r w:rsidRPr="00366B53">
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体"/>
</w:rPr>
<w:t xml:space="preserve"> 漏洞描述</w:t>
<w:br/>
<w:t xml:space="preserve"/>
<w:br/>
<w:t xml:space="preserve"> 下面是个图片:</w:t>
<w:br/>
<w:t xml:space="preserve"> ![Description](http://localhost:8000/static/a89deb2306f24cdcb5e54020b17ad010.png)</w:t>
<w:br/>
<w:t xml:space="preserve"/>
<w:br/>
<w:t xml:space="preserve"> 下面是个图片2:</w:t>
<w:br/>
<w:t xml:space="preserve"/>
<w:br/>
<w:t xml:space="preserve"> ![Description](http://localhost:8000/static/573bdd8094d5464f81d985cc712861b2.png)</w:t>
<w:br/>
<w:t xml:space="preserve"/>
<w:br/>
<w:t xml:space="preserve"> 下面是个图片3:</w:t>
<w:br/>
<w:t xml:space="preserve"> ![Description](http://localhost:8000/static/c886cfd0704a4a5cb0ce3374ec2613da.png)</w:t>
<w:br/>
<w:t xml:space="preserve"/>
</w:r>
</w:p>

The \n from the markdown is converted to <w:t>, but I don't know how to locate the position to the correct <w:t> element and then add_picture to it.

I search the web and checked most of the issues of python-docx github, still no luck.

Any idea how to achieve this?

Pls help, very much appreciate!

BTW, I'm using docxtpl to generate this docx, here's what the template looks like: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source