-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
81 lines (51 loc) · 2.15 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
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
from typing import List
from PyPDF2 import PdfFileReader, PdfFileWriter
from arguments import argparse_setup
def create_pdf(filename: str):
pdf_writer = PdfFileWriter()
with open(filename, "wb") as file:
pdf_writer.write(file)
def append_pages_to_pdf(page_indexes: List[int], input_pdf_name: str, output_pdf_name: str):
pdf_writer = get_all_pages(output_pdf_name)
pdf = PdfFileReader(input_pdf_name)
for index in page_indexes:
pdf_writer.addPage(pdf.getPage(index - 1))
write_pdf(output_pdf_name, pdf_writer)
print(f"Appended page(s) from {input_pdf_name} to {output_pdf_name}")
def remove_pages_from_pdf(page_indexes: List[int], pdf_name: str):
pdf = PdfFileReader(pdf_name)
pdf_writer = PdfFileWriter()
for index in range(pdf.getNumPages()):
if index + 1 not in page_indexes:
pdf_writer.addPage(pdf.getPage(index))
write_pdf(pdf_name, pdf_writer)
print(f"Removed page(s) from {pdf_name}")
def duplicate_pdf(input_pdf_name: str, output_pdf_name: str = ""):
pdf = PdfFileReader(input_pdf_name)
pdf_writer = PdfFileWriter()
for index in range(pdf.getNumPages()):
pdf_writer.addPage(pdf.getPage(index))
if output_pdf_name == "":
input_basename = input_pdf_name.split(".pdf")[0]
output_pdf_name = f"{input_basename}(1).pdf"
write_pdf(output_pdf_name, pdf_writer)
print(f"Duplicated {input_pdf_name}")
def get_all_pages(pdf_name: str) -> PdfFileWriter:
pdf = PdfFileReader(pdf_name)
pdf_writer = PdfFileWriter()
for page in range(pdf.getNumPages()):
pdf_writer.addPage(pdf.getPage(page))
return pdf_writer
def write_pdf(filename: str, pdf_writer: PdfFileWriter):
with open(filename, "wb") as out_pdf:
pdf_writer.write(out_pdf)
if __name__ == "__main__":
args = argparse_setup()
if args.create:
create_pdf(args.output_file)
if args.remove:
remove_pages_from_pdf(args.page_indexes, args.input_file)
if args.duplicate:
duplicate_pdf(args.input_file, args.output_file)
if args.append:
append_pages_to_pdf(args.page_indexes, args.input_file, args.output_file)