-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfMerge.py
79 lines (62 loc) · 2.38 KB
/
pdfMerge.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
from PyPDF2 import PdfFileWriter, PdfFileReader
'''
# 删除文件中的一页
output = PdfFileWriter()
input = PdfFileReader(open("F:/1.pdf", "rb"))
num = input.getNumPages()
for i in range(num):
if i!=0:
output.addPage(input.getPage(i))
# finally, write "output" to document-output.pdf
outputStream = open("F:/out.pdf", "wb")
output.write(outputStream)
outputStream.close()
'''
# 将两个PDF的所有页聚合在一起
output = PdfFileWriter()
input1 = PdfFileReader(open("F:/2.pdf", "rb"))
input2 = PdfFileReader(open("F:/out.pdf", "rb"))
# add page 1 from input1 to output document, unchanged
output.appendPagesFromReader(input1)
output.appendPagesFromReader(input2)
# output.addPage(input1.getPage(0))
# output.addPage(input2.getPage(0))
# finally, write "output" to document-output.pdf
outputStream = open("F:/o.pdf", "wb")
output.write(outputStream)
outputStream.close()
'''
from PyPDF2 import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
input1 = PdfFileReader(open("document1.pdf", "rb"))
# print how many pages input1 has:
print("document1.pdf has %d pages." % input1.getNumPages())
# add page 1 from input1 to output document, unchanged
output.addPage(input1.getPage(0))
# add page 2 from input1, but rotated clockwise 90 degrees
output.addPage(input1.getPage(1).rotateClockwise(90))
# add page 3 from input1, rotated the other way:
output.addPage(input1.getPage(2).rotateCounterClockwise(90))
# alt: output.addPage(input1.getPage(2).rotateClockwise(270))
# add page 4 from input1, but first add a watermark from another PDF:
page4 = input1.getPage(3)
watermark = PdfFileReader(open("watermark.pdf", "rb"))
page4.mergePage(watermark.getPage(0))
output.addPage(page4)
# add page 5 from input1, but crop it to half size:
page5 = input1.getPage(4)
page5.mediaBox.upperRight = (
page5.mediaBox.getUpperRight_x() / 2,
page5.mediaBox.getUpperRight_y() / 2
)
output.addPage(page5)
# add some Javascript to launch the print window on opening this PDF.
# the password dialog may prevent the print dialog from being shown,
# comment the the encription lines, if that's the case, to try this out
output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# encrypt your new PDF and add a password
password = "secret"
output.encrypt(password)
# finally, write "output" to document-output.pdf
outputStream = file("PyPDF2-output.pdf", "wb")
output.write(outputStream)'''