-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (43 loc) · 1.57 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
import os
from PyPDF2 import PdfFileMerger
def merger(pdfs, result_path):
"""
get pdf paths and writes to another path the result
@param pdfs: paths to pdf files.
@type pdfs: C{list} of C{str}
@param result_path: the path where to save the merged-pdf.
@type result_path: C{str}
"""
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write(result_path)
def main(pdf_paths, result_path):
merger(pdf_paths, result_path)
def parse_arguments():
parser = argparse.ArgumentParser(description='Process some integers.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-f', "--folder", dest="pdfs_folder", type=str,
help='folder of pdf files')
group.add_argument('-s', "--pdfs", type=str, nargs="+",
help='list of pdf files')
parser.add_argument('output', type=str,
help='path to store the merged file')
args = parser.parse_args()
def dir_dir(name):
return [os.path.join(name, x) for x in os.listdir(name)]
pdfs = []
if args.pdfs_folder is not None:
pdfs_folder = os.path.abspath(args.pdfs_folder)
if not os.path.isdir(pdfs_folder):
parser.error("folder must be a folder")
else:
pdfs = dir_dir(pdfs_folder)
else:
pdfs = [os.path.abspath(x) for x in args.pdfs]
if not any(pdfs):
parser.error("None pdf files have been found.")
return pdfs, args.output
if __name__ == '__main__':
main(*parse_arguments())