Python 3 库可将任何图像合并为 PDF

Raf*_*Raf 5 python pdf pypdf

在python 3中,我有一个各种格式的图像列表(pdf、png、jpg、gif),我将它们全部合并到一个多页pdf中。

使用PyPDF2,可以合并 PDF 文件。但不支持 png、jpg 等。此处对此进行了很好的介绍: 合并 PDF 文件

使用img2pdf,可以将 png、jpg 等图像类型转换为 PDF 并进行合并。但是,它不支持输入 PDF 文件。此处: 从图像列表创建 PDF

因此,由于我可以将 PDF、PNG、JPG 作为输入,因此我习惯这样处理它:

from PyPDF2 import PdfFileMerger
import img2pdf

if not ext == 'pdf':
    with open("output.pdf", "wb") as f:
        f.write(img2pdf.convert(images))
else:
    merger = PdfFileMerger()
    for pdf in images:    
        merger.append(pdf)
    merger.write("output.pdf")
Run Code Online (Sandbox Code Playgroud)

问题是:我是否需要这 2 个库来将一系列图像(包括 PDF)合并到一个 PDF 中?换句话说,是否有一个库可以将任何图像(包括 PDF)作为输入,并将它们全部合并为一个 PDF?

小智 1

我知道这个问题已经被问了很长时间了,但我想分享这个我在想要的时候找到的答案,所以同样的事情,以防万一其他人将来遇到这个问题。PyMuPDF 模块提供与 PyPDF2 模块类似的功能,但具有一些 PyPDF2 所不具备的功能(包括此功能)。根据 PyMuPDF 的文档,这里有一些代码可以解决这个问题。它确实使用了 PySimpleGUI 模块作为用户界面,但如果您愿意,可以将其删除。

import os, fitz
import PySimpleGUI as psg  # for showing a progress bar
doc = fitz.open()  # PDF with the pictures
imgdir = "path-to-picture-directory"  # where the pics are
imglist = os.listdir(imgdir)  # list of them
imgcount = len(imglist)  # pic count

for i, f in enumerate(imglist):
    img = fitz.open(os.path.join(imgdir, f))  # open pic as document
    rect = img[0].rect  # pic dimension
    pdfbytes = img.convert_to_pdf()  # make a PDF stream
    img.close()  # no longer needed
    imgPDF = fitz.open("pdf", pdfbytes)  # open stream as PDF
    page = doc.new_page(width = rect.width,  # new page with ...
                   height = rect.height)  
# pic dimension
     page.show_pdf_page(rect, imgPDF, 0)  
# image fills the page
    psg.EasyProgressMeter("Import Images",  # show our progress
    i+1, imgcount)

 doc.save("all-my-pics.pdf")
Run Code Online (Sandbox Code Playgroud)

您可以在该模块的文档网站上找到此代码以及有关 PyMuPDF 的更多信息,可以在此处找到: https: //pymupdf.readthedocs.io/en/latest/recipes-images.html#how-to-make-one -您所有图片或文件的 pdf

我希望这对您或将来遇到此问题的人有所帮助!