'Convert images in multiple subfolders into pdf
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
Solution 1:[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.convertand/orsend2trashdo not take pathlike objects, but expectstrinstead, just wrapfilenamein anstrcall. E.g.send2trash(str(filename))By using
pathlibinstead of constructing the paths/filenames yourself, is that it works cross-platform, whereas your code would not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
