Python 是否在文档(.docx)中的特定位置添加图像?
我使用Python docx生成Microsoft Word文档。用户在为例如“各位早上好,这是我的%(profile_img)s您喜欢它吗?” 在HTML字段中,我创建了一个word文档,并从数据库中重现了用户的图片,然后用用户的图片替换关键字%(profile_img),而不是在文档的末尾。对于Python docx,我们使用以下指令添加图片:Python 是否在文档(.docx)中的特定位置添加图像?,python,image,python-2.7,python-docx,Python,Image,Python 2.7,Python Docx,我使用Python docx生成Microsoft Word文档。用户在为例如“各位早上好,这是我的%(profile_img)s您喜欢它吗?” 在HTML字段中,我创建了一个word文档,并从数据库中重现了用户的图片,然后用用户的图片替换关键字%(profile_img),而不是在文档的末尾。对于Python docx,我们使用以下指令添加图片: document.add_picture('profile_img.png', width=Inches(1.25)) 图片被添加到文档中,但问题
document.add_picture('profile_img.png', width=Inches(1.25))
图片被添加到文档中,但问题是它被添加到文档末尾。
是否无法使用python在microsoft word文档中的特定位置添加图片?我在网上还没有找到任何答案,但在其他地方看到有人问同样的问题,却没有解决办法
谢谢(注意:我不是一个经验丰富的程序员,除了这个尴尬的部分,我的其余代码将非常基本)引用:
add_picture()方法将指定的图片添加到文档末尾的段落中。然而,通过深入挖掘API,您可以将文本放在图片段落的任意一侧,或者两者都放
当我们“深入挖掘”时,我们发现了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')
引述:
add_picture()方法将指定的图片添加到文档末尾的段落中。然而,通过深入挖掘API,您可以将文本放在图片段落的任意一侧,或者两者都放
当我们“深入挖掘”时,我们发现了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')
我不知道这是否适用于您,但以下是我为将特定位置的图像设置为docx文档所做的工作: 我创建了一个基本docx文档(模板文档)。在这个文件中,我插入了一些无边框的表,用作图像的占位符。创建文档时,首先打开模板,更新文件,在表中创建图像。因此,代码本身与原始代码没有太大区别,唯一的区别是我在一个特定的表中创建了段落和图像
from docx import Document
from docx.shared import Inches
doc = Document('addImage.docx')
tables = doc.tables
p = tables[0].rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('resized.png',width=Inches(4.0), height=Inches(.7))
p = tables[1].rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('teste.png',width=Inches(4.0), height=Inches(.7))
doc.save('addImage.docx')
我不知道这是否适用于您,但以下是我为将特定位置的图像设置为docx文档所做的工作: 我创建了一个基本docx文档(模板文档)。在这个文件中,我插入了一些无边框的表,用作图像的占位符。创建文档时,首先打开模板,更新文件,在表中创建图像。因此,代码本身与原始代码没有太大区别,唯一的区别是我在一个特定的表中创建了段落和图像
from docx import Document
from docx.shared import Inches
doc = Document('addImage.docx')
tables = doc.tables
p = tables[0].rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('resized.png',width=Inches(4.0), height=Inches(.7))
p = tables[1].rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('teste.png',width=Inches(4.0), height=Inches(.7))
doc.save('addImage.docx')
这是我的解决办法。它在第一个命题上的优势是,它用标题(样式标题1)和用于附加注释的部分围绕图片。请注意,您必须按照Word文档中显示的相反顺序进行插入
from docx import Document
from docx.shared import Inches
# ------- initial code -------
document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_text('Good Morning every body,This is my ')
picPath = 'D:/Development/Python/aa.png'
r.add_picture(picPath)
r.add_text(' do you like it?')
document.save('demo.docx')
# ------- improved code -------
document = Document()
p = document.add_paragraph('Picture bullet section', 'List Bullet')
p = p.insert_paragraph_before('')
r = p.add_run()
r.add_picture(picPath)
p = p.insert_paragraph_before('My picture title', 'Heading 1')
document.save('demo_better.docx')
如果要以编程方式在现有文档中插入图片,此代码段尤其有用
from docx import Document
from docx.shared import Inches
# ------- initial code -------
document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_text('Good Morning every body,This is my ')
picPath = 'D:/Development/Python/aa.png'
r.add_picture(picPath)
r.add_text(' do you like it?')
document.save('demo.docx')
# ------- improved code -------
document = Document()
p = document.add_paragraph('Picture bullet section', 'List Bullet')
p = p.insert_paragraph_before('')
r = p.add_run()
r.add_picture(picPath)
p = p.insert_paragraph_before('My picture title', 'Heading 1')
document.save('demo_better.docx')
这是我的解决办法。它在第一个命题上的优势是,它用标题(样式标题1)和用于附加注释的部分围绕图片。请注意,您必须按照Word文档中显示的相反顺序进行插入
from docx import Document
from docx.shared import Inches
# ------- initial code -------
document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_text('Good Morning every body,This is my ')
picPath = 'D:/Development/Python/aa.png'
r.add_picture(picPath)
r.add_text(' do you like it?')
document.save('demo.docx')
# ------- improved code -------
document = Document()
p = document.add_paragraph('Picture bullet section', 'List Bullet')
p = p.insert_paragraph_before('')
r = p.add_run()
r.add_picture(picPath)
p = p.insert_paragraph_before('My picture title', 'Heading 1')
document.save('demo_better.docx')
如果要以编程方式在现有文档中插入图片,此代码段尤其有用
from docx import Document
from docx.shared import Inches
# ------- initial code -------
document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_text('Good Morning every body,This is my ')
picPath = 'D:/Development/Python/aa.png'
r.add_picture(picPath)
r.add_text(' do you like it?')
document.save('demo.docx')
# ------- improved code -------
document = Document()
p = document.add_paragraph('Picture bullet section', 'List Bullet')
p = p.insert_paragraph_before('')
r = p.add_run()
r.add_picture(picPath)
p = p.insert_paragraph_before('My picture title', 'Heading 1')
document.save('demo_better.docx')
这是罗布写的答案ᵩ 同时考虑用户更灵活的输入。 我的假设是Kais Dkhili(原始查询器)提到的HTML字段已经加载到
docx.Document()
中。所以
确定相关HTML文本在文档中的位置。
将所需图像替换为标识为img\u tag='%(profile\u img)s'
以下代码是在考虑到文本只包含一个run
如果条件另有规定,则可相应更改
temp_text = img_tag.split(img_paragraph.text)
img_paragraph.runs[0].text = temp_text[0]
_r = img_paragraph.add_run()
_r.add_picture('profile_img.png', width = Inches(1.25))
img_paragraph.add_run(temp_text[1])
完成了<代码>文档.保存()如果最终确定,则保存它
如果您想知道从temp\u文本中可以得到什么
[In]
img_tag.split(img_paragraph.text)
[Out]
['This is my ', ' do you like it?']
这是罗布写的答案ᵩ 同时考虑用户更灵活的输入。 我的假设是Kais Dkhili(原始查询器)提到的HTML字段已经加载到
docx.Document()
中。所以
确定相关HTML文本在文档中的位置。
将所需图像替换为标识为img\u tag='%(profile\u img)s'
以下代码是在考虑到文本只包含一个run
如果条件另有规定,则可相应更改
temp_text = img_tag.split(img_paragraph.text)
img_paragraph.runs[0].text = temp_text[0]
_r = img_paragraph.add_run()
_r.add_picture('profile_img.png', width = Inches(1.25))
img_paragraph.add_run(temp_text[1])
完成了<代码>文档.保存()如果最终确定,则保存它
如果您想知道从temp\u文本中可以得到什么
[In]
img_tag.split(img_paragraph.text)
[Out]
['This is my ', ' do you like it?']
我在里面呆了几个小时。如果需要使用python将图像添加到模板文档文件中,最好的解决方案是使用python docx模板库。 文件可用
在中提供的示例我花了几个小时在上面。如果需要使用python将图像添加到模板文档文件中,最好的解决方案是使用python docx模板库。 文件可用
中提供的示例如果您先将文本的左侧部分添加到关键短语,然后添加图像,然后添加文本的其余部分,会怎么样?你试过了吗?你好,伦茨,我试过了,但结果是第一行的文本“早上好,各位,这是我的”,第二行的文本“你喜欢吗”,文档末尾的图片如果你先将文本的左侧部分添加到关键短语,然后添加图像,然后添加其余文本,会怎么样?你试过这个吗?你好,伦茨,是的,我试过了,但结果是“各位早上好,我