我有将图像插入到DOCX模板中的工作代码(使用docxtpl提供的Jinja模板格式),但是,当图像插入到文档正文中时,图像无法在标题中呈现。文档{{p my_image}}中的插入点显示消息“Read Error”。
我假设这是python-docx库中的错误/限制,但我想知道是否有已知的解决方法。我已经尝试了两种方法来创建图像,但都失败了,只有在头文件中使用时才会出现相同的“Read Error”消息:
方法1:
sub_doc = self.template.new_subdoc()
p = sub_doc.add_paragraph()
r = p.add_run()
r.add_picture(output_path)
return sub_doc方法二:
from docxtpl import InlineImage
return InlineImage(template, image_path, width, height)发布于 2020-11-27 16:32:27
这是个老问题,但也许有人需要它。
我使用DOCX模块将图像写入到docx文件的头文件中。在我的例子中,我必须将图像写入到一个表中。
from docx import Document
from docx.shared import Inches
header = doc.sections[0].header
#Insert table with 2 rows and 3 columns
headertable = header.add_table(2, 3, Inches(6))
headertable.style("borderColor:black")
a = headertable.cell(0, 0)
b = headertable.cell(1, 0)
A = a.merge(b)
#add table in header
headertab_cells = headertable.rows[0].cells
ht0 = headertab_cells[0].add_paragraph()
kh = ht0.add_run()
kh.add_picture('logo.jpg', width=Inches(1))https://stackoverflow.com/questions/54283818
复制相似问题