python中如何让图形出现在指定位置_使用Python在文档(.docx)中的特定位置添加图像?...

1586010002-jmsa.png

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

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)

解决方案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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值