-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.py
154 lines (129 loc) · 5.14 KB
/
pdf.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from fpdf import FPDF
import os
from tkinter import *
import sys
root = Tk()
tf = Frame(root)
bf = Frame(root)
root.title("PDF Maker")
checkBlank = IntVar()
checkCombo = IntVar()
checkBlank.set(0)
checkCombo.set(0)
dir_path = os.path.dirname(os.path.realpath(__file__))
def get_immediate_subdirectories(a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def main(blank, combo):
folders = []
chapters = []
images = []
errors = []
num_errors = 0
folder_name = ""
folders = (get_immediate_subdirectories(os.getcwd()))
#Debug print
#print(folders)
#print("these are the folders ^")
if combo==0:
for f in folders:
if (f[:2] == "Ch" or f[:3] =='vol' or f[:3] =='Vol' or f[:2] == "ch"):
images = []
image_names = []
images = os.listdir(os.path.join(os.getcwd() + "\\" + f))
#Debug prints
#print(images)
#print("These are the images")
#print(f)
for img in images:
image_names.append(img[:-4])
i = 1
#print(image_names)
pdf = FPDF()
#If the user checks the blank page box then there will be a blank page
if blank==1:
pdf.add_page()
#print(images.__len__())
while (i < images.__len__()):
try:
pdf.set_font('Arial', 'B', 16)
#print(i)
full_image_name = str(images[image_names.index(str(i))])
image_path = os.path.join(os.getcwd() + "\\" + f + "\\" + full_image_name)
pdf.add_page()
#Debug print
#print("adding image "+full_image_name)
pdf.image(image_path, x=0, y=0, w=190, )
except:
#print("Unexpected error:", sys.exc_info()[0])
num_errors=num_errors+1
full_image_name = str(i)
#errors.append(full_image_name + " from "+f)
#pdf.cell(40, 10, "Error adding image " + full_image_name + " from " + f)
print("Error adding image " + full_image_name + " from " + f)
i = i + 1
try:
pdf.close()
pdf.output(f + ".pdf", "F")
except:
print("Error closing/Saving PDF :'(")
if num_errors>0:
print("Finished with "+str(num_errors))
else:
print("Finished with no errors :D")
else:
pdf = FPDF()
pdf.set_font('Arial', 'B', 16)
for f in folders:
if (f[:2] == "Ch" or f[:3] == 'vol' or f[:2] == "ch"):
images = []
image_names = []
images = os.listdir(os.path.join(os.getcwd() + "\\" + f))
# Debug prints
# print(images)
# print("These are the images")
# print(f)
for img in images:
image_names.append(img[:-4])
i = 1
# print(image_names)
# If the user checks the blank page box then there will be a blank page
if blank == 1:
pdf.add_page()
print(images.__len__())
while (i < images.__len__()):
try:
# print(i)
full_image_name = str(images[image_names.index(str(i))])
image_path = os.path.join(os.getcwd() + "\\" + f + "\\" + full_image_name)
pdf.add_page()
# Debug print
# print("adding image "+full_image_name)
pdf.image(image_path, x=0, y=0, w=190, )
except:
# print("Unexpected error:", sys.exc_info()[0])
num_errors = num_errors + 1
full_image_name = str(i)
# errors.append(full_image_name + " from "+f)
# pdf.cell(40, 10, "Error adding image " + full_image_name + " from " + f)
# print("Error adding image " + full_image_name + " from " + f)
i = i + 1
try:
pdf.close()
pdf.output("Combo.pdf", "F")
except:
print("Error closing/Saving PDF :'(")
if num_errors > 0:
print("Finished with " + str(num_errors))
else:
print("Finished with no errors :D")
def mainFunc():
main(checkBlank.get(), checkCombo.get())
bf.pack(side=BOTTOM)
tf.pack()
checkBox1 = Checkbutton(bf, variable=checkBlank, onvalue=1, offvalue=0, text="First page Blank?").pack()
checkBox2 = Checkbutton(bf, variable=checkCombo, onvalue=1, offvalue=0, text="Make into one pdf?").pack()
button1 = Button(tf, text="Make PDF", width=45, command=mainFunc)
button1.pack()
root.mainloop()
print("All done")