Simplified README, added folder for sample PyPDF2 code

This commit is contained in:
Matthew Stamy 2013-10-08 17:11:06 -05:00
parent 38a8c3a0ee
commit 5141360584
4 changed files with 88 additions and 64 deletions

78
README
View File

@ -1,67 +1,17 @@
Example 1:
PyPDF2
-------------------------------------------------
from PyPDF2 import PdfFileWriter, PdfFileReader
PyPDF2 is a pure-python PDF library capable of
splitting, merging together, cropping, and transforming
the pages of PDF files. It can also add custom
data, viewing options, and passwords to PDF files.
It can retrieve text and metadata from PDFs as well
as merge entire files together.
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)
# encrypt your new PDF and add a password
password = "secret"
output.encrypt(password)
# finally, write "output" to document-output.pdf
outputStream = file("document-output.pdf", "wb")
output.write(outputStream)
Example 2:
from PyPDF2 import PdfFileReader, 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)
See sample code folder for helpful examples.
Documentation: <URL coming soon>
FAQ: <URL coming soon>
PyPI: <https://pypi.python.org/pypi/PyPDF2>
GitHub: <https://github.com/mstamy2/PyPDF2>
Homepage (needs updating): <http://mstamy2.github.io/PyPDF2/>

14
Sample_Code/README.txt Normal file
View File

@ -0,0 +1,14 @@
PyPDF2 Sample Code Folder
-------------------------
This will contain demonstrations of the many features
PyPDF2 is capable of. Example code should make it easy
for users to know how to use all aspects of PyPDF2.
Feel free to add any type of PDF file or sample code,
either by
1) sending it via email to PyPDF2@phaseit.net
2) including it in a pull request on GitHub

View File

@ -0,0 +1,40 @@
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)
# 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)

View File

@ -0,0 +1,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)