|
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/> |
0 commit comments