在python中将图像文件从附件转换为Pdf文件

时间:2016-08-01 22:54:11

标签: python python-3.x pdf

我正在尝试从.msg文件的附件转换图像并保存到PdF文件中。但是,当我尝试读取图像文件以转换为PdF文件时,我收到错误。这是我的代码的一部分

if count_attachments > 0:
    for item in range(count_attachments):          
        attached = msg.Attachments.Item(item + 1)            
        extension = attached.filename.split(".")[-1]

        if extension == 'jpg' or extension == 'png':
            pp = PdfPages(newname)
            img_data = open(attached, 'rb').read()                
            pp.savefig(img_data)                
            pp.close()

这是我从编译器中得到的错误

Traceback (most recent call last):
File "email-reader1.py", line 52, in <module>
img_data = open(attached, 'rb').read()
TypeError: Can't convert 'CDispatch' object to str implicitly

1 个答案:

答案 0 :(得分:0)

更换线:
img_data = open(attached, 'rb').read()

使用:
img_data = open(attached.filename, 'rb').read()