"""
用途:将一个 PDF 文件按页拆分为多个 PDF 文件
注意事项:若报错 ModuleNotFoundError: No module named 'PyPDF2'
则需要先安装该模块。使用命令 “pip install PyPDF2” 安装即可
"""
import PyPDF2
import os
root = r'C:\Users\liujieru\Documents\pdf'
file_path = os.path.join(root, '组合.pdf')
pdf_file = open(file_path, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
source_name = pdf_file.name
pdf_writer = PyPDF2.PdfFileWriter()
for page_num in range(pdf_reader.numPages):
page_obj = pdf_reader.getPage(page_num)
pdf_writer.addPage(page_obj)
new_name = source_name[:-4] + str(page_num) + ".pdf"
pdf_new_file = open(new_name, 'wb')
pdf_writer.write(pdf_new_file)
pdf_new_file.close()
pdf_writer.__init__()
pdf_file.close()