贴一个分割的:
[Python] 纯文本查看 复制代码 from PyPDF2 import PdfReader, PdfWriter
import time
def subsplit(curpage, targetpage, pageCount, reader, output_name):
print(f"split {curpage}-{targetpage}")
output = PdfWriter()
outfile = f"{output_name}_{curpage}_{targetpage}.pdf" # 分割后的文件
while curpage < targetpage and curpage < pageCount:
output.add_page(reader.pages[curpage])
curpage += 1
# 写入到目标PDF文件
outputStream = open(outfile, "wb")
output.write(outputStream)
outputStream.close()
def split(input_file, output_name, subfile_page_task):
reader = PdfReader(open(input_file, "rb"))
pageCount = len(reader.pages)
curPage = 0
target_task = 0
target_task_num = 0
while curPage < pageCount:
if target_task < len(subfile_page_task):
target_task_num = subfile_page_task[target_task]
else:
target_task_num = pageCount
subsplit(curPage, target_task_num, pageCount, reader, output_name)
target_task += 1
curPage = target_task_num
if __name__ == '__main__':
time1 = time.time()
inputf1 = "D:\\BaiduNetdiskDownload\\2025版《初中必刷题》数学 RJ 7上.pdf" # 待分割文件
outputf1 = "《初中必刷题》数学RJ7上" # 分割后的文件名
f1subfile_page_task = [44, 91, ]
split(inputf1, outputf1, f1subfile_page_task)
inputf2 = "d:\\BaiduNetdiskDownload\\2025版《初中必刷题》数学 RJ 7上 -批注式详答与详析.pdf" # 待分割文件
outputf2 = "《初中必刷题》数学RJ7上-批注式详答与详析" # 分割后的文件名
f2subfile_page_task = [35, ]
split(inputf2, outputf2, f2subfile_page_task)
inputf3 = "d:\\BaiduNetdiskDownload\\2025版《初中必刷题》数学 RJ 7上 -狂K重点.pdf" # 待分割文件
outputf3 = "《初中必刷题》数学RJ7上-狂K重点" # 分割后的文件名
f3subfile_page_task = [33, ]
split(inputf3, outputf3, f3subfile_page_task)
time2 = time.time()
print('time consumed %s s.' % (time2 - time1)) |