willygroup
willygroup

Reputation: 323

python embed image in RTF

I've a small Python program that edit an RTF template. I need to embed ad image in a specific position of the rtf file

I've found this snip of code for png images (originally I think was in C#):

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

I don't know which library can let me to convert the image in the right format, someone can give me some tips?

Thanks a lot, Willy

Upvotes: 1

Views: 1373

Answers (1)

willygroup
willygroup

Reputation: 323

It's a little intricate but works :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+'}')
...

Upvotes: 2

Related Questions