-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
61 lines (46 loc) · 1.48 KB
/
split.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
import os
from Pdf import PdfSize, PdfParts
from Errors import TooFewPagesError, EmptyFileError
from input_sanitizer import sanitize_input
from mensagens import PASTA_JA_EXISTE, TOO_FEW_PAGES, EMPTY_FILE
def get_pdf_instance(mode, pdf_args):
sop, f, outpath = pdf_args
if mode == "tamanho":
return PdfSize(sop, f, outpath)
else:
try:
p = PdfParts(sop, f, outpath)
except TooFewPagesError:
print(TOO_FEW_PAGES)
raise TooFewPagesError
return p
def create_outfolder(outpath):
try:
os.mkdir(outpath)
except FileExistsError:
print(PASTA_JA_EXISTE)
raise FileExistsError
def split_pdf(pdf_args, outpath):
sop, filepath, mode = pdf_args
with open(filepath, "rb") as f:
p = get_pdf_instance(mode, (sop, f, outpath))
try:
p.split()
except EmptyFileError:
print(EMPTY_FILE)
raise EmptyFileError
def list_selected_files(path):
if path.endswith(".pdf"):
return [path]
else:
return [os.path.join(path, file) for file in os.listdir(path) if file.endswith(".pdf")]
def split_infiles(sop, path, mode):
for filepath in list_selected_files(path):
outpath = os.path.join(str(filepath)[:-4])
create_outfolder(outpath)
split_pdf((sop, filepath, mode), outpath)
def main():
sop, path, mode = sanitize_input()
split_infiles(sop, path, mode)
if __name__ == "__main__":
main()