Advertisement
steve-shambles-2109

184-Folder of images to pdf

Oct 22nd, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. """
  2. Python code snippets vol 37:
  3. 184-Folder of images to pdf
  4. stevepython.wordpress.com
  5.  
  6. requirements:
  7. pip3 install fpdf
  8. pip3 install pillow
  9.  
  10. source:
  11. https://stackoverflow.com/questions/27327513/create-pdf-from-a-list-of-images
  12. """
  13. import os
  14. from fpdf import FPDF
  15. from PIL import Image
  16.  
  17.  
  18. listPages = os.listdir("images")
  19.  
  20. def makePdf(pdfFileName, listPages, dir=''):
  21.     if dir:
  22.         dir += "/"
  23.  
  24.     cover = Image.open(dir + str(listPages[0]))
  25.     width, height = cover.size
  26.  
  27.     pdf = FPDF(unit="pt", format=[width, height])
  28.  
  29.     for page in listPages:
  30.         pdf.add_page()
  31.         pdf.image(dir + str(page), 0, 0)
  32.  
  33.     pdf.output(pdfFileName + ".pdf", "F")
  34.  
  35. makePdf('output.pdf', listPages, 'images')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement