【发布时间】:2015-07-21 04:48:42
【问题描述】:
我想知道是否有一种简单的方法可以在 python 中将多个 png 图像组合成一个 pdf。我希望每张图片都是 pdf 中的单页。 pypdf 是最好的库吗?任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
根据我的经验,ImageMagick 总是通过几行代码为我提供所需的内容。
我想知道是否有一种简单的方法可以在 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)
【讨论】:
WKHtmlToPdf 有一个 python 端口:
https://pypi.python.org/pypi/wkhtmltopdf/0.1
使用 css 可以轻松地在 html 文档中的 img 标记之间创建分页符,您可以将其传递给此库。
【讨论】:
from PIL import Image
image1 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi24.jpg')
image2 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi-1.jpg')
image3 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/SNP_1291.jpg')
im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')
imagelist = [im1,im2,im3]
im1.save(r'C:/Users/uidg3972/Desktop/New folder/mergedImages.pdf',save_all=True,
append_images=imagelist)
【讨论】:
【讨论】: