File: basic_merging.py

package info (click to toggle)
pypdf2 1.23%2Bgit20141008-1%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 608 kB
  • ctags: 654
  • sloc: python: 3,646; makefile: 26; sh: 17
file content (20 lines) | stat: -rw-r--r-- 608 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from PyPDF2 import PdfFileMerger

merger = PdfFileMerger()
     
input1 = open("document1.pdf", "rb")
input2 = open("document2.pdf", "rb")
input3 = open("document3.pdf", "rb")

# add the first 3 pages of input1 document to output
merger.append(fileobj = input1, pages = (0,3))

# insert the first page of input2 into the output beginning after the second page
merger.merge(position = 2, fileobj = input2, pages = (0,1))

# append entire input3 document to the end of the output document
merger.append(input3)

# Write to an output PDF document
output = open("document-output.pdf", "wb")
merger.write(output)