How can I convert all JPG files in a folder to PDFs and combine them?

后端 未结 1 1874
轻奢々
轻奢々 2021-01-03 13:57

I have a few 1924 × 2972 JPG files in a folder (/img/). I want to 1) Convert them to PDFs and 2) Combine them into a single PDF. But I am not successful. I am new to python.

相关标签:
1条回答
  • 2021-01-03 14:09

    In your second loop, you need to actually reference the path to each jpg:

    pdf.image(image, 10,210,297)
    

    Edit: additionally, I think you just need the path to each image (rather than opening each image using Image.open. Try the following:

    import glob
    from fpdf import FPDF #
    imagelist = glob.glob('img/*.jpg')
    
    pdf = FPDF()
    # imagelist is the list with all image filenames
    for image in imagelist:
        pdf.add_page()
        pdf.image(image, 10,210,297)
    pdf.output("yourfile.pdf", "F")
    
    0 讨论(0)
提交回复
热议问题