python在RTF中嵌入图像

2024-09-29 23:26:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我发现了png图像的代码片段(最初我认为是用C#)编写的:

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

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

非常感谢, 威利


Tags: 文件代码图像程序模板编辑imgpng
1条回答
网友
1楼 · 发布于 2024-09-29 23:26:42

有点复杂,但很管用

...
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+'}')
...

相关问题 更多 >

    热门问题