0

I am trying to deal with any image extension to convert any image to pdf (in any subfolder) Here's my try

from pathlib import Path
from PyPDF2 import PdfFileMerger
import os

BASE_DIR = Path.cwd()
MAIN_DIR = BASE_DIR / 'MAIN'

for subfolder in os.listdir(MAIN_DIR):
    if os.path.isdir(MAIN_DIR + subfolder):
        for filename in os.listdir(MAIN_DIR + subfolder):
            if filename.endswith(('.jpg', '.JPG')):
                filename_regex = re.compile(r'(\.jpg)|(\.jpeg)', re.IGNORECASE)
                new_name = filename_regex.sub('', filename)
                f = open(MAIN_DIR + subfolder + '/' + new_name + '.pdf', 'wb')
                f.write(img2pdf.convert(MAIN_DIR + subfolder + '/' + filename))
                send2trash(MAIN_DIR + subfolder + '/' + filename)

But this throws an error

Traceback (most recent call last):
  File "C:\Users\Future\Desktop\test.py", line 9, in <module>
    if os.path.isdir(MAIN_DIR + subfolder):
TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'

How can I deal with pathlib as for joining the main directory to the filename?

After testing the code of Doczero, I encountered an error

C:\Users\Future\Desktop\MAIN\3\Sample.jpg
Traceback (most recent call last):
  File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\img2pdf.py", line 2229, in convert
    rawdata = img.read()
AttributeError: 'WindowsPath' object has no attribute 'read'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Future\Desktop\test.py", line 15, in <module>
    f.write(img2pdf.convert(filename))
  File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\img2pdf.py", line 2232, in convert
    raise TypeError("Neither implements read() nor is str or bytes")
TypeError: Neither implements read() nor is str or bytes
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 1
    When asking questions here, it's best to provide a [mre] which means taking out everything that's unrelated to the problem. You also shouldn't change your question or add another to it after initially posting it. – martineau Mar 20 '22 at 14:24

1 Answers1

1

This is my take on your code, replacing calls to os by the pathlib module:

main_dir = Path(Path.cwd(),'MAIN')

for subfolder in main_dir.iterdir():
    # Iterate over all contents in the main folder
    if subfolder.is_file():
        # The item is a file, not a folder, ignore
        continue
        
    for filename in subfolder.iterdir():
        # Iterate over all items in the subfolder
        if filename.suffix.lower() in ['.jpg', '.jpeg']:
            # The filename is a jpg
            # Write to a file with the same name, ending in PDF instead
            with open(filename.with_suffix('.pdf'), 'wb') as f:
                f.write(img2pdf.convert(filename))
                send2trash(filename)

Notes:

  • if img2pdf.convert and/or send2trash do not take pathlike objects, but expect str instead, just wrap filename in an str call. E.g. send2trash(str(filename))

  • By using pathlib instead of constructing the paths/filenames yourself, is that it works cross-platform, whereas your code would not.

DocZerø
  • 8,037
  • 11
  • 38
  • 66