无法在PDF中附加多个图像文件

时间:2018-09-03 09:38:29

标签: python-3.x image pdf

我知道关于这个问题的答案,但是请听我说。

我目前正在尝试使用python中的img2pdf从.jpg文件中提取PDF,但是没有将文件附加到PDF,而是覆盖了PDF中已经存在的页面。

这是代码

import os,img2pdf

os.chdir("/home/aditya/Desktop")#images are inside desktop

root, dir, files = list(os.walk(os.getcwd()))[0]#files contains the 
                                                 list of all names of all .jpg file
                                                 which I want to convert into PDF

with open("pdf_file.pdf","ab") as f:#PDF file is set to append
    for img_file in files:
        with open(img_file,"rb") as im_file:#read bytes from the image files
            f.write(img2pdf.convert(im_file))#this line overwrites the exisiting
                                              pages in the pdf despite the fact that 
                                              I have set it to #append

任何原因吗?我需要传递特殊属性吗?

感谢您的帮助。谢谢

0 个答案:

没有答案