0

I am using a Python script to batch convert many images in different folders into single pdfs (with https://pypi.org/project/img2pdf/):

import os
import subprocess
import img2pdf 
from shutil import copyfile

def main():
    folders = [name for name in os.listdir(".") if os.path.isdir(name)] 
    for f in folders:
        files = [f for f in os.listdir(f)] 
        p = ""
        for ffile in files:
            p += f+'\\'  + ffile + " "                
        os.system("py -m img2pdf *.pn* " + p + " --output " + f + "\combined.pdf")
    
        
if __name__ == '__main__':
    main()

However, despite running the command via Powershell on Windows 10, and despite using very short filenames, when the number of images is very high (eg over 600 or so), Powershell gives me the error "The command line is too long" and it does not create the pdf. I know there is a command-line string limitation (https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/command-line-string-limitation), but I also know that for powershell this limit is higher (Powershell to avoid cmd 8191 character limit), and I can't figure out how to fix the script. I would like to ask you if you could help me fix the script to avoid violating the character limit. Thank you

PS: I use the script after inserting it in the parent folder that contains the folders with the images; then in each subfolder the output pdf file is created.

qwertxyz
  • 143
  • 1
  • 7
  • can you break up your array into multiple smaller arrays and call the app more than once? – Ctznkane525 Jul 09 '21 at 17:36
  • 2
    If you use `img2pdf` as a library, you can avoid having to deal with the command line (and its limitations). – Scott Hunter Jul 09 '21 at 17:38
  • I could try, but I'm not very experienced and I don't know how to do it: could you suggest me the changes to be made to the code? – qwertxyz Jul 09 '21 at 17:40
  • The project description page has some examples. But if you aren't even willing to try, I'll spend my time helping those who are. – Scott Hunter Jul 09 '21 at 20:35
  • Yes, I have seen, but unfortunately I know very little about python: I have not created the script. – qwertxyz Jul 09 '21 at 20:47

1 Answers1

1

Using img2pdf library you can use this script:

import img2pdf
import os

for r, _, f in os.walk("."):
    imgs = []
    for fname in f:
        if fname.endswith(".jpg") or fname.endswith(".png"):
            imgs.append(os.path.join(r, fname))
    if len(imgs) > 0:
        with open(r+"\output.pdf","wb") as f:
            f.write(img2pdf.convert(imgs))