微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PyPDF2 PDF生成工具

程序名称:PyPDF2

授权协议: BSD

操作系统: 跨平台

开发语言: Python

PyPDF2 介绍

PyPDF2一个纯 Python PDF 库,能够分割、合并、裁剪和转换 PDF 文件页面。它还可以添加自定义数据、查看选项和密码到 PDF 文件

PyPDF2 可以从 PDF 中检索文本和元数据,也可以将整个文件合并在一起。

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, unchangedoutput.addPage(input1.getPage(0))# add page 2 from input1, but rotated clockwise 90 degreesoutput.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)# add some Javascript to launch the print window on opening this PDF.# the password dialog may prevent the print dialog from being shown,# comment the the encription lines, if that's the case, to try this outoutput.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")# encrypt your new PDF and add a passwordpassword = "secret"output.encrypt(password)# finally, write "output" to document-output.pdfoutputStream = file("PyPDF2-output.pdf", "wb")
output.write(outputStream)

PyPDF2 官网

https://github.com/mstamy2/PyPDF2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐