从图像列表创建PDF [英] Create PDF from a list of images

查看:56
本文介绍了从图像列表创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用Python从图像文件列表中创建PDF的实用方法?

Is there any practical way to create a PDF from a list of images files, using Python?

在Perl中,我知道该模块.有了它,我可以只用3行创建一个PDF:

In Perl I know that module. With it I can create a PDF in just 3 lines:

use PDF::FromImage;
...
my $pdf = PDF::FromImage->new;
$pdf->load_images(@allPagesDir);
$pdf->write_file($bookName . '.pdf');

我需要做与此非常相似的事情,但是要用Python.我知道 pyPdf 模块,但是我想简单些.

I need to do something very similar to this, but in Python. I know the pyPdf module, but I would like something simple.

@编辑

如果您是通过Google来的,则代码如下:

If you came through Google, here's the code:

from fpdf import FPDF
from PIL import Image
def makePdf(pdfFileName, listPages, dir = ''):
    if (dir):
        dir += "/"

    cover = Image.open(dir + str(listPages[0]) + ".jpg")
    width, height = cover.size

    pdf = FPDF(unit = "pt", format = [width, height])

    for page in listPages:
        pdf.add_page()
        pdf.image(dir + str(page) + ".jpg", 0, 0)

    pdf.output(dir + pdfFileName + ".pdf", "F")

推荐答案

安装用于Python的FPDF :

pip install fpdf

现在您可以使用相同的逻辑:

Now you can use the same logic:

from fpdf import FPDF
pdf = FPDF()
# imagelist is the list with all image filenames
for image in imagelist:
    pdf.add_page()
    pdf.image(image,x,y,w,h)
pdf.output("yourfile.pdf", "F")

您可以在教程页面或官方文档.

这篇关于从图像列表创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆