【问题标题】:Could not append multiple image files in a PDF无法在 PDF 中附加多个图像文件
【发布时间】:2019-02-08 08:26:03
【问题描述】:

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

我目前正在尝试在 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

有什么理由吗?我需要传递什么特殊属性吗?

感谢任何帮助。谢谢

【问题讨论】:

    标签: python-3.x image pdf


    【解决方案1】:

    img2pdf.convert 不会逐个图像转换。它要么做一个单一的图像,要么一次全部完成

    img2pdf.convert(list of images )
    
    with open("pdf_file.pdf","ab") as f: #Open a pdf 
         f.write(img2pdf.convert(files)) #convert all the images and write bytes 
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2018-11-27
      • 2021-06-25
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      相关资源
      最近更新 更多