如何使用 Python docx 库在 MS Word 文档的左上角添加徽标?

How to add a logo on the top left of MS Word document using the Python docx library?

我正在尝试使用 python-docx 库在我的 Word 文档的左上角添加一个小徽标。当我尝试添加图像时,使用代码:

doc.add_picture('logo.png', width=Inches(1.2))

好像加到我的word文档末尾了。谁能帮忙解决这个问题

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.

所以你需要add_rundoc

因此,要添加到文档的开头,您必须首先向文档添加 paragraph,然后将该图片添加到 paragraph

document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_picture('logo.png', width=Inches(1.2))