Python-docx:如何将 StringIO 图像添加到 docx 文件?

Python-docx: How to add StringIO image to a docx file?

我可以使用此代码将图像插入 docx 文件

from docx import Document

document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_picture('/tmp/foo.jpg')

但是如果我将图像作为 StringIO 对象。不另存为文件怎么办?

P:S: 我使用的库是 python-docx

让我们快速阅读一下python-docx documentation

add_picture(image_path_or_stream, width=None, height=None)

因此,您应该将流直接传递给此函数。

image = BytesIO()
r.add_picture(image)