我想要做的是使用 Python 将图像插入到现有 Word 文档中的特定位置。我已经查看了各种图书馆来做到这一点;我正在使用 docx-mailmerge 包通过 Word 合并字段插入文本和表格,但不幸的是,图像合并只是一个待办事项/愿望清单功能。 python-docx 同时允许插入图像,但只能在文档末尾,而不是在特定位置。
有没有其他图书馆可以做到这一点,或者有什么好的技巧来完成它?
答案 0 :(得分:1)
摆弄底层 API(感谢 this SO answer)我成功了:
[ChartImage1]
比如:
document = Document("template.docx")
image_paras = [i for i, p in enumerate(document.paragraphs) if "[ChartImage1]" in p.text]
p = document.paragraphs[image_paras[0]]
p.text = ""
r = p.add_run()
r.add_picture("path/to/image.png")
document.save("my_doc.docx")