Skip to content

Commit 5141360

Browse files
committed
Simplified README, added folder for sample PyPDF2 code
1 parent 38a8c3a commit 5141360

File tree

4 files changed

+91
-67
lines changed

4 files changed

+91
-67
lines changed

README

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,17 @@
1-
Example 1:
2-
3-
from PyPDF2 import PdfFileWriter, PdfFileReader
4-
5-
output = PdfFileWriter()
6-
input1 = PdfFileReader(open("document1.pdf", "rb"))
7-
8-
# print how many pages input1 has:
9-
print "document1.pdf has %d pages." % input1.getNumPages()
10-
11-
# add page 1 from input1 to output document, unchanged
12-
output.addPage(input1.getPage(0))
13-
14-
# add page 2 from input1, but rotated clockwise 90 degrees
15-
output.addPage(input1.getPage(1).rotateClockwise(90))
16-
17-
# add page 3 from input1, rotated the other way:
18-
output.addPage(input1.getPage(2).rotateCounterClockwise(90))
19-
# alt: output.addPage(input1.getPage(2).rotateClockwise(270))
20-
21-
# add page 4 from input1, but first add a watermark from another PDF:
22-
page4 = input1.getPage(3)
23-
watermark = PdfFileReader(open("watermark.pdf", "rb"))
24-
page4.mergePage(watermark.getPage(0))
25-
output.addPage(page4)
26-
27-
28-
# add page 5 from input1, but crop it to half size:
29-
page5 = input1.getPage(4)
30-
page5.mediaBox.upperRight = (
31-
page5.mediaBox.getUpperRight_x() / 2,
32-
page5.mediaBox.getUpperRight_y() / 2
33-
)
34-
output.addPage(page5)
35-
36-
# encrypt your new PDF and add a password
37-
password = "secret"
38-
output.encrypt(password)
39-
40-
# finally, write "output" to document-output.pdf
41-
outputStream = file("document-output.pdf", "wb")
42-
output.write(outputStream)
43-
44-
45-
Example 2:
46-
47-
from PyPDF2 import PdfFileReader, PdfFileMerger
48-
49-
merger = PdfFileMerger()
50-
51-
input1 = open("document1.pdf", "rb")
52-
input2 = open("document2.pdf", "rb")
53-
input3 = open("document3.pdf", "rb")
54-
55-
# add the first 3 pages of input1 document to output
56-
merger.append(fileobj = input1, pages = (0,3))
57-
58-
# insert the first page of input2 into the output beginning after the second page
59-
merger.merge(position = 2, fileobj = input2, pages = (0,1))
60-
61-
# append entire input3 document to the end of the output document
62-
merger.append(input3)
63-
64-
# Write to an output PDF document
65-
output = open("document-output.pdf", "wb")
66-
merger.write(output)
67-
1+
PyPDF2
2+
-------------------------------------------------
3+
4+
PyPDF2 is a pure-python PDF library capable of
5+
splitting, merging together, cropping, and transforming
6+
the pages of PDF files. It can also add custom
7+
data, viewing options, and passwords to PDF files.
8+
It can retrieve text and metadata from PDFs as well
9+
as merge entire files together.
10+
11+
See sample code folder for helpful examples.
12+
13+
Documentation: <URL coming soon>
14+
FAQ: <URL coming soon>
15+
PyPI: <https://pypi.python.org/pypi/PyPDF2>
16+
GitHub: <https://github.com/mstamy2/PyPDF2>
17+
Homepage (needs updating): <http://mstamy2.github.io/PyPDF2/>

Sample_Code/README.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
PyPDF2 Sample Code Folder
2+
-------------------------
3+
4+
This will contain demonstrations of the many features
5+
PyPDF2 is capable of. Example code should make it easy
6+
for users to know how to use all aspects of PyPDF2.
7+
8+
9+
10+
Feel free to add any type of PDF file or sample code,
11+
either by
12+
13+
1) sending it via email to PyPDF2@phaseit.net
14+
2) including it in a pull request on GitHub

Sample_Code/basic_features.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from PyPDF2 import PdfFileWriter, PdfFileReader
2+
3+
output = PdfFileWriter()
4+
input1 = PdfFileReader(open("document1.pdf", "rb"))
5+
6+
# print how many pages input1 has:
7+
print "document1.pdf has %d pages." % input1.getNumPages()
8+
9+
# add page 1 from input1 to output document, unchanged
10+
output.addPage(input1.getPage(0))
11+
12+
# add page 2 from input1, but rotated clockwise 90 degrees
13+
output.addPage(input1.getPage(1).rotateClockwise(90))
14+
15+
# add page 3 from input1, rotated the other way:
16+
output.addPage(input1.getPage(2).rotateCounterClockwise(90))
17+
# alt: output.addPage(input1.getPage(2).rotateClockwise(270))
18+
19+
# add page 4 from input1, but first add a watermark from another PDF:
20+
page4 = input1.getPage(3)
21+
watermark = PdfFileReader(open("watermark.pdf", "rb"))
22+
page4.mergePage(watermark.getPage(0))
23+
output.addPage(page4)
24+
25+
26+
# add page 5 from input1, but crop it to half size:
27+
page5 = input1.getPage(4)
28+
page5.mediaBox.upperRight = (
29+
page5.mediaBox.getUpperRight_x() / 2,
30+
page5.mediaBox.getUpperRight_y() / 2
31+
)
32+
output.addPage(page5)
33+
34+
# encrypt your new PDF and add a password
35+
password = "secret"
36+
output.encrypt(password)
37+
38+
# finally, write "output" to document-output.pdf
39+
outputStream = file("PyPDF2-output.pdf", "wb")
40+
output.write(outputStream)

Sample_Code/basic_merging.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from PyPDF2 import PdfFileMerger
2+
3+
merger = PdfFileMerger()
4+
5+
input1 = open("document1.pdf", "rb")
6+
input2 = open("document2.pdf", "rb")
7+
input3 = open("document3.pdf", "rb")
8+
9+
# add the first 3 pages of input1 document to output
10+
merger.append(fileobj = input1, pages = (0,3))
11+
12+
# insert the first page of input2 into the output beginning after the second page
13+
merger.merge(position = 2, fileobj = input2, pages = (0,1))
14+
15+
# append entire input3 document to the end of the output document
16+
merger.append(input3)
17+
18+
# Write to an output PDF document
19+
output = open("document-output.pdf", "wb")
20+
merger.write(output)

0 commit comments

Comments
 (0)