PyPDF的前身是PyPDF包在2005年发布,该包的最后一个版本发布于2010年,后来大约经过一年左右,名为Phasit的公司赞助PyPDF的一个分支后来命名为PyPDF2,两个版本功能都基本一样,最大的区别就是PyPDF2中加入了支持Python3功能。后面又出现了PyPDF3、PyPDF4等不同版本,但这些包并没有对PyPDF2功能向后完全兼容,受欢迎程度当然也不如PyPDF2。
使用命令:pip install pypdf2。
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test01 # Software : PyCharm # Note : 使用PyPDF2模块将单个PDF拆分为多个PDF文件 # 导入模块 from PyPDF2 import PdfFileReader, PdfFileWriter # pdf 文档 pdf_name = "test.pdf" pdf_reader = PdfFileReader(pdf_name) # PDF页数 page_num = pdf_reader.getNumPages() i_count = 0 # 计数 for i in range(0, page_num, 20): # 每20页拆分成一个新的PDF文件 i_count += 1 pdf_writer = PdfFileWriter() for j in range(i, min(i + 20, page_num)): pdf_writer.addPage(pdf_reader.getPage(j)) save_pdf_name = str(i_count).zfill(3) + ".pdf" with open(save_pdf_name, "wb") as fo: pdf_writer.write(fo)
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test02 # Software : PyCharm # Note : 使用PyPDF2模块将多个PDF合并为一个PDF文件 # 导入模块 from PyPDF2 import PdfFileReader, PdfFileWriter # 需要合并的PDF文件名列表 merge_pdf_names = ["001.pdf", "002.pdf", "003.pdf", "004.pdf"] merge_writer = PdfFileWriter() # 遍历处理每一个PDF文件 for pdf_name in merge_pdf_names: curr_reader = PdfFileReader(pdf_name) page_num = curr_reader.getNumPages() for i in range(page_num): merge_writer.addPage(curr_reader.getPage(i)) with open("merge.pdf", "wb") as fo: merge_writer.write(fo)
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test03 # Software : PyCharm # Note : 使用PyPDF2模块对PDF添加水印 # 导入模块 from PyPDF2 import PdfFileReader, PdfFileWriter # pdf 文档 pdf_name = "test.pdf" # 水印pdf 文档 water_mark = "watermark.pdf" # 加完水印的pdf 文档 new_pdf_name = "test_watermark.pdf" water_mark_page = PdfFileReader(water_mark).getPage(0) pdf_reader = PdfFileReader(pdf_name) pdf_writer = PdfFileWriter() page_num = pdf_reader.getNumPages() for i in range(page_num): page = pdf_reader.getPage(i) page.mergePage(water_mark_page) pdf_writer.addPage(page) with open(new_pdf_name, "wb") as fo: pdf_writer.write(fo)
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test04 # Software : PyCharm # Note : 使用PyPDF2模块对PDF文件加密 # 导入模块 from PyPDF2 import PdfFileReader, PdfFileWriter # pdf 文档 pdf_name = "test.pdf" # 加密后的pdf 文档 new_pdf_name = "test_encryption.pdf" pdf_reader = PdfFileReader(pdf_name) pdf_writer = PdfFileWriter() page_num = pdf_reader.getNumPages() for i in range(page_num): page = pdf_reader.getPage(i) pdf_writer.addPage(page) # 加密 pdf_writer.encrypt(user_pwd="mayi", use_128bit=True) with open(new_pdf_name, "wb") as fo: pdf_writer.write(fo)
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test05 # Software : PyCharm # Note : 使用PyPDF2模块对PDF文件解密 # 导入模块 from PyPDF2 import PdfFileReader, PdfFileWriter # pdf 文档 pdf_name = "test_encryption.pdf" # 解密后的pdf 文档 new_pdf_name = "test.pdf" # 密码 pass_word = "mayi" pdf_reader = PdfFileReader(pdf_name) # 解密 pdf_reader.decrypt(pass_word) pdf_writer = PdfFileWriter() page_num = pdf_reader.getNumPages() for i in range(page_num): page = pdf_reader.getPage(i) pdf_writer.addPage(page) with open(new_pdf_name, "wb") as fo: pdf_writer.write(fo)
使用PyPDF2可以从PDF中提取到一些元数据和文本信息,对PDF有个大致了解。
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : MaYi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2022-08-19 # Name : test06 # Software : PyCharm # Note : 使用PyPDF2模块获取PDF文件的基本信息 # 导入模块 from PyPDF2 import PdfFileReader # pdf 文档 pdf_name = "test.pdf" with open(pdf_name, 'rb') as f: pdf = PdfFileReader(f) # 获取PDF文件的基本信息 infomation = pdf.getDocumentInfo() # 作者 author = infomation.author # 创建者 creator = infomation.creator # 制作者 producer = infomation.producer # Subject(主题) subject = infomation.subject # 标题 title = infomation.title # 页数 page_num = pdf.getNumPages() # 打印获取的基本信息 print("作者:%s\t创建者:%s\t制作者:%s\t主题:%s\t标题:%s\t页数:%s" % (author, creator, producer, subject, title, page_num))