-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
30 lines (22 loc) · 958 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import img2pdf
# Global Vars
n = 3 # Chunk size (number of images per PDF file)
count = 1 # File counter
photolist = [] # Declared empty list
path = 'A:\Python Projects\Image to PDF\photos' # Images location
f_ext = '.jpg' # Image file extension
os.chdir(path)
for each in os.listdir():
if each.endswith(f_ext):
photolist.append(each)
newlist = [photolist[i * n:(i + 1) * n] for i in range((len(photolist) + n - 1) // n)]
for each in newlist:
converted = img2pdf.convert(each)
name = str(count) + '.pdf'
file = open(name, 'wb')
file.write(converted)
file.close()
print('Saved {} PDF file(s)' .format(count))
count += 1
print('All images were converted to PDF files successfully.')