img2pdf_with_package.py 764 B

123456789101112131415161718192021222324
  1. import img2pdf
  2. from Image2PDF.config import SOURCE_DIR_PATH, TARGET_DIR_PATH
  3. import os
  4. def imgs_from_dir2pdf(source_dir_path, target_dir_path):
  5. # convert all files ending in .jpg inside a directory
  6. dirname = source_dir_path
  7. imgs = []
  8. for fname in os.listdir(dirname):
  9. if not fname.endswith(".jpg"):
  10. if not fname.endswith(".png"):
  11. continue
  12. path = os.path.join(dirname, fname)
  13. if os.path.isdir(path):
  14. continue
  15. imgs.append(path)
  16. for count,each_img in enumerate(imgs):
  17. with open(target_dir_path+r"\Img{}.pdf".format(count), "wb") as f:
  18. f.write(img2pdf.convert(each_img))
  19. if __name__=='__main__':
  20. imgs_from_dir2pdf(SOURCE_DIR_PATH, TARGET_DIR_PATH)