Это старая версия документа.


Библиотека на чистом Python для работы с PDF. Вот на что способен модуль:

  • извлечение информации из документа (название, автор, …)
  • постраничное разрезание документа,
  • постраничное объединение документа,
  • обрезка страниц,
  • merging multiple pages into a single page,
  • encrypting and decrypting PDF files.

By being Pure-Python, it should run on any Python platform without any dependencies on external libraries. It can also work entirely on StringIO objects rather than file streams, allowing for PDF manipulation in memory. It is therefore a useful tool for websites that manage or manipulate PDFs.

Пакет pyPDF доступен из официального репозитория: этот замечательный пакет

Простой пример

example.py
from pyPdf import PdfFileWriter, PdfFileReader
 
output = PdfFileWriter()
input1 = PdfFileReader(file("document1.pdf", "rb"))
 
# выводим на экран заголовок файла document1.pdf
print "title = %s" % (input1.getDocumentInfo().title)
 
# добавляем в output страницу №1 из inpu1, без изменений
output.addPage(input1.getPage(0))
 
# добавляем в output страницу №2 из inpu1, но перевернутую на 90 градусов
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(file("watermark.pdf", "rb"))
page4.mergePage(watermark.getPage(0))
 
# 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)
 
# print how many pages input1 has:
print "document1.pdf has %s pages." % input1.getNumPages()
 
# finally, write "output" to document-output.pdf
outputStream = file("document-output.pdf", "wb")
output.write(outputStream)
outputStream.close()

Ссылки