使用Python在文档(.docx)中的特定位置添加图像? [英] Add an image in a specific position in the document (.docx) with Python?

查看:5662
本文介绍了使用Python在文档(.docx)中的特定位置添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python-docx生成Microsoft Word文档。当用户写作时,用户想要这样:早安,每个人,这是我的%(profile_img)你喜欢吗?
在HTML字段中,我创建一个word文档,我从数据库中恢复用户的图片,然后用用户的图片替换关键字%(profile_img)s NOT在END OF文件。使用Python-docx,我们使用此指令添加图片:

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 HTML field, i create a word document and i recuper the picture of the user from the database and i replace the key word %(profile_img)s by the picture of the user NOT at the END OF THE DOCUMENT. With Python-docx we use this instruction to add a picture:

document.add_picture('profile_img.png', width=Inches(1.25))

图片被添加到文档中但是最后添加的问题该文件。
使用python在microsoft word文档中的特定位置添加图片是不可能的?我没有在网上找到任何答案,但看到有人在其他地方要求同样没有解决方案。

The picture is added to the document but the problem that it is added at the end of the document. Is it impossible to add a picture in a specific position in a microsoft word document with python? I've not found any answers to this in the net but have seen people asking the same elsewhere with no solution.

谢谢(注意:我不是一个很大的经验程序员,除了这个尴尬的部分,其余的代码将非常基本)

Thanks (note: I'm not a hugely experiance programmer and other than this awkward part the rest of my code will very basic)

推荐答案

引用 python-docx文档


Document.add_picture()方法将指定的图片添加到文档末尾的段落中。但是,通过深入挖掘API,您可以将文本放在图片的两侧,或两者中。

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.

当我们深入挖掘时,我们会发现 Run.add_picture() API。

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

以下是其示例使用:

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')

这篇关于使用Python在文档(.docx)中的特定位置添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆