我想知道是否有一种简单的方法可以在 python 中将多个 png 图像组合成一个 pdf。我希望每张图片都是 pdf 中的一页。 pypdf 是用于此的最佳库吗?任何帮助将不胜感激。
谢谢!
最佳答案
我也遇到了同样的问题。
所以我创建了 python 函数来将多张图片合并到一个 pdf 中。 代码可在我的 github 页面上找到 https://github.com/wizard1989/Unite-multiple-pictures-into-pdf . 它使用“reportlab”。
代码基于以下链接的答案:
Create PDF from a list of images
Combining multiple pngs in a single pdf in python
png images to one pdf in python
How can I convert all JPG files in a folder to PDFs and combine them?
https://www.blog.pythonlibrary.org/2012/01/07/reportlab-converting-hundreds-of-images-into-pdfs/
这里是如何将图像合并为 pdf 的示例。
我们有文件夹“D:\pictures”,其中包含 png 和 jpg 类型的图片。 我们想从中创建文件 pdf_with_pictures.pdf 并将其保存在同一文件夹中。
outputPdfName = "pdf_with_pictures"
pathToSavePdfTo = "D:\\pictures"
pathToPictures = "D:\\pictures"
splitType = "none"
numberOfEntitiesInOnePdf = 1
listWithImagesExtensions = ["png", "jpg"]
picturesAreInRootFolder = True
nameOfPart = "volume"
unite_pictures_into_pdf(outputPdfName, pathToSavePdfTo, pathToPictures, splitType, numberOfEntitiesInOnePdf, listWithImagesExtensions, picturesAreInRootFolder, nameOfPart)
https://stackoverflow.com/questions/30154846/