首页 > 解决方案 > python在RTF中嵌入图像

问题描述

我有一个编辑 RTF 模板的小型 Python 程序。我需要在 rtf 文件的特定位置嵌入广告图片

我找到了 png 图像的这段代码(最初我认为是在 C# 中):

mpic = "{\pict\pngblip\picw" + img_Width + "\pich" + img_Height + "\picwgoal" + width + @"\pichgoal" + height + "\bin " + str + "}"

我不知道哪个库可以让我将图像转换为正确的格式,有人可以给我一些提示吗?

非常感谢,威利

标签: pythonrtf

解决方案


这有点复杂但有效:D

...
filename = 'temp.png'
hex_content = ''
from PIL import Image
    im = Image.open(filename)
    width, height = im.size
    im.close()
with open(filename, 'rb') as f:
    content = f.read()
    hex_content = binascii.hexlify(content)
    f.close()
    file_content = file_content.replace("<#LEAKS_IMAGE>",'{\pict\pngblip\picw'+str(width)+'\pich'+str(height)+'\picwgoal10000\pichgoal8341 '+hex_content+'}')
...

推荐阅读