-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_merger.py
127 lines (97 loc) · 3.68 KB
/
pdf_merger.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
from dataclasses import dataclass
import os
import re
from argparse import ArgumentParser
from typing import List, Tuple
from datetime import datetime
from PyPDF2 import errors, PdfReader, PdfMerger
VERSION = "2.0.0"
@dataclass
class Args:
files: List[str]
output_file_name: str
file_name_bookmarks: bool
import_bookmarks: bool
quiet: bool
def load_args() -> Args:
parser = ArgumentParser(description=f"PDF Merger v{VERSION}")
parser.add_argument("-f", "--file",
dest="files",
nargs='+',
required=True,
default=f"output_{datetime.now().strftime('%Y%m%d%H%M%S')}.pdf",
metavar="[file name]",
help="PDF files to merge")
parser.add_argument("-o", "--output",
dest="output_file_name",
default=f"output_{datetime.now().strftime('%Y%m%d%H%M%S')}.pdf",
metavar="[file name]",
help="Output filename")
parser.add_argument("-d", "--disable-file-name-bookmarks",
dest="file_name_bookmarks",
action="store_false",
default=True,
help="File names will not be added to bookmarks")
parser.add_argument("-D", "--no-bookmark-import",
dest="import_bookmarks",
action="store_false",
default=True,
help="Doesn't import bookmarks from source PDFs")
parser.add_argument("-q", "--quiet",
dest="quiet",
action="store_true",
default=False,
help="No text will be written to stdout")
args = parser.parse_args()
if len(vars(args)) == 0:
parser.print_help()
exit(1)
return Args(**vars(args))
def get_selection(file_list: list) -> List[str]:
selected_files: List[str] = []
regex_a = re.compile(r"(\/|\\|^)\*.pdf$")
regex_b = re.compile(r"(\/|\\|^)\*(.\*|)$")
for file in file_list:
if regex_a.search(file): # *.pdf
folder = os.path.dirname(file)
if not folder:
folder = "./"
for f in os.listdir(folder):
if len(f) >= 4 and f[-4:] == ".pdf":
selected_files.append(os.path.join(folder, f))
elif regex_b.search(file): # *, *.*
folder = os.path.dirname(file)
if not folder:
folder = "./"
for f in os.listdir(folder):
selected_files.append(os.path.join(folder, f))
else:
selected_files.append(file)
return selected_files
def print_q(text: str, quiet: bool):
if not quiet:
print(text)
def create(args: Args):
valid_pdfs: List[Tuple[str, str]] = []
for file in get_selection(args.files):
with open(file, "rb") as s:
try:
_ = PdfReader(s)
valid_pdfs.append((file, os.path.basename(file).replace(".pdf", "")))
except errors.PdfReadError:
print_q(f"\u001b[33;1m{file} is not a valid pdf. This file will be skipped!\u001b[0m", args.quiet)
except IOError:
print_q(f"\u001b[31;1m{file} thrown an IOError. This file will be skipped!\u001b[0m", args.quiet)
pdf_merger = PdfMerger()
try:
for pdf_file, pdf_name in valid_pdfs:
if args.file_name_bookmarks:
pdf_merger.append(pdf_file, pdf_name, import_outline=args.import_bookmarks)
else:
pdf_merger.append(pdf_file, import_outline=args.import_bookmarks)
pdf_merger.write(args.output_file_name)
finally:
pdf_merger.close()
print_q(f"'{args.output_file_name}' file has been successfully created!", args.quiet)
if __name__ == "__main__":
create(load_args())