-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_pdfs.py
58 lines (48 loc) · 1.63 KB
/
merge_pdfs.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
import os, appex, PyPDF2, shutil, glob, console, sys
DIRNAME = "pdf_merging_directory"
os.makedirs(DIRNAME, exist_ok=True)
os.chdir(DIRNAME)
def __extract_filenum(filename):
try:
return int(filename.partition(".")[0])
except ValueError:
raise ValueError("extraneous file slipped in, make sure DIRNAME is an empty or nonexistent directory")
def __get_next_counter():
files = glob.glob("*.pdf")
if len(files) == 0:
return 0
return max([__extract_filenum(x) for x in files]) + 1
def __add_file_to_queue(infilename):
outfilename = str(__get_next_counter()) + ".pdf"
with open(infilename, "rb") as infile:
bytes = infile.read()
with open(outfilename, "wb") as outfile:
outfile.write(bytes)
def __merge_all_pdfs():
dest = PyPDF2.PdfFileMerger()
for count in range(__get_next_counter()):
filename = str(count) + ".pdf"
try:
with open(filename, "rb") as pdf:
dest.append(PyPDF2.PdfFileReader(pdf))
except FileNotFoundError:
raise FileNotFoundError("Somehow one of the files you queued up is missing!")
with open("outfile.pdf", "wb") as outfile:
dest.write(outfile)
console.open_in("outfile.pdf")
def __cleanup():
os.chdir("..")
shutil.rmtree(DIRNAME)
def add_file():
infile = appex.get_file_path()
__add_file_to_queue(infile)
def finalize_pdf():
infile = appex.get_file_path()
__add_file_to_queue(infile)
__merge_all_pdfs()
__cleanup()
choice = console.alert("PDF Merge", "", "Add to queue", "Finalize PDF")
if choice == 1:
add_file()
if choice == 2:
finalize_pdf()