python在RTF中嵌入图像

时间:2018-11-28 15:06:06

标签: python rtf

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

我已经找到了用于png图片的代码片段(最初我认为是在C#中):

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

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

非常感谢, 威利

1 个答案:

答案 0 :(得分:0)

有点复杂,但是可以: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+'}')
...