Add an image in a specific position in the document (.docx) with Python?

前端 未结 4 1201
一生所求
一生所求 2020-12-03 08:30

I use Python-docx to generate Microsoft Word document.The user want that when he write for eg: \"Good Morning every body,This is my %(profile_img)s do you like it?\" in a HT

4条回答
  •  孤城傲影
    2020-12-03 08:49

    Quoting the python-docx documentation:

    The Document.add_picture() method adds a specified picture to the end of the document in a paragraph of its own. However, by digging a little deeper into the API you can place text on either side of the picture in its paragraph, or both.

    When we "dig a little deeper", we discover the Run.add_picture() API.

    Here is an example of its use:

    from docx import Document
    from docx.shared import Inches
    
    document = Document()
    
    p = document.add_paragraph()
    r = p.add_run()
    r.add_text('Good Morning every body,This is my ')
    r.add_picture('/tmp/foo.jpg')
    r.add_text(' do you like it?')
    
    document.save('demo.docx')
    

提交回复
热议问题