简单可重复的工作,坚决不做第二次,通通写个脚本自动化
安装
直接使用 pip 安装就可以了
pip install PyPDF2
PyPDF2 包含了 PdfFileReader PdfFileMerger PageObject PdfFileWriter 四个常用的主要 Class。
简单读写 PDF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from PyPDF2 import PdfFileReader, PdfFileWriter infn = 'infn.pdf' outfn = 'outfn.pdf'
pdf_input = PdfFileReader(open(infn, 'rb'))
page_count = pdf_input.getNumPages() print(page_count)
page = pdf_input.getPage(i)
pdf_output = PdfFileWriter()
pdf_output.addPage(page)
pdf_output.write(open(outfn, 'wb'))
|
应用实例 合并分割 PDF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| from PyPDF2 import PdfFileReader, PdfFileWriter
def split_pdf(infn, outfn): pdf_output = PdfFileWriter() pdf_input = PdfFileReader(open(infn, 'rb')) page_count = pdf_input.getNumPages() print(page_count) for i in range(5, page_count): pdf_output.addPage(pdf_input.getPage(i)) pdf_output.write(open(outfn, 'wb'))
def merge_pdf(infnList, outfn): pdf_output = PdfFileWriter() for infn in infnList: pdf_input = PdfFileReader(open(infn, 'rb')) page_count = pdf_input.getNumPages() print(page_count) for i in range(page_count): pdf_output.addPage(pdf_input.getPage(i)) pdf_output.write(open(outfn, 'wb'))
if __name__ == '__main__': infn = 'infn.pdf' outfn = 'outfn.pdf' split_pdf(infn, outfn)
|
应用实例源代码可以在 https://github.com/xchaoinfo/Py-example-by-xchaoinfo 找到。
Refer: PyPDF2 Documentation