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