-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_gen.py
68 lines (42 loc) · 1.45 KB
/
pdf_gen.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
import PyPDF2
def parser(num_range):
"""
This function parses the num_range to num list in
reversed order
Args:
num_range(str): num ranges in the format of x-y
Return:
list: The reverse list of the num_range
Example:
print parser(20-25)
return [24, 23, 22, 21, 20, 19]
"""
index_list = [int(item) for item in num_range.split("-")]
#Pages range in reversed order
return [i for i in range(index_list[0]-1, index_list[1])][::-1]
def print_pdf(input_file, output_file, page_range):
"""
This function generates a new pdf with desired output filename,
by inserting the input filename and page range that you want to print
Args:
input_file(str): input pdf filename
output_file(str): output pdf filename
page_range(str): page range in the format of x-y
Returns:
void
Example:
print_pdf("/foo/bar.pdf", "/foo/new_bar.pdf", 20-25)
"""
pdf_file = open(input_file, "r")
read_pdf = PyPDF2.PdfFileReader(pdf_file, strict=False)
#meta_data = read_pdf.documentInfo
try:
new_pdf = open(output_file, "w")
write_pdf = PyPDF2.PdfFileWriter()
#write_pdf.addMetadata(meta_data)
for item in parser(page_range):
write_pdf.insertPage(read_pdf.getPage(item))
write_pdf.write(new_pdf)
print "%s Generated" % output_file
except IndexError:
print "Page not Found!"