0

I'm trying to convert a list of files to a single pdf in python. I started by following the answer in this post, but I had the issue that some of the files I want to include in my pdf are already pdfs, while others are jpgs or other image files, and Pillow doesn't directly support reading pdf.

I currently have this

from PIL import Image
from pdf2image import convert_from_path
def pdfsort(filename):
   if(filename[-3:].lower()=="pdf"):
      return convert_from_path(filename)
   else:
      return(filename)
    
images = [
   Image.open(pdfsort("dir"+f))
   for f in ["file1.jpg","file2.pdf","file3.jpg"]
]

I'm getting the error message "AttributeError: 'list' object has no attribute 'read'". I imagine this is caused by my function. Is there a better way of getting this done?

user2895444
  • 59
  • 1
  • 2
  • 6
  • 1
    `convert_from_path` returns a list of images, so the problem likely comes from passing that list to `Image.open`. See https://github.com/Belval/pdf2image#how-does-it-work – slothrop Jun 19 '23 at 15:40
  • It seems like it's probably easier to split your file list into a list of PDFs and a list of non-PDFs. For the non-PDFs, use something like your current list comprehension; for the PDFs, just use `convert_from_path` and you've got your images. – slothrop Jun 19 '23 at 15:41
  • I ended up just breaking it into a for loop since I wanted to maintain the order of the images. Thanks for the tip about the output, @slothrop. – user2895444 Jun 19 '23 at 19:21

0 Answers0