根据页码范围从pdf文档中提取并另存为新pdf文档

发布于 2023-10-17  245 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

使用PyPDF2第三方Python包

from PyPDF2 import PdfReader, PdfWriter
import os
import copy

filename = "xxxx.pdf"
reader = PdfReader(filename)
savepath = "C://BinY//OneDrive - xxx.ca//xxx//"

num_page = reader.pages
outline = reader.outline

intro = outline[9]


pdf_writer = PdfWriter()
start = 19
st = copy.deepcopy(start)
end = 32

while start <= end:
    pdf_writer.add_page(reader.pages[start -1])
    start+=1

if not os.path.exists(savepath):
    os.makedirs(savepath)

output_filename = savepath + '{}_{}_page_{}.pdf'.format(filename[:-4], st,end)
with open(output_filename,'wb') as out:
    pdf_writer.write(out)
Everything not saved will be lost.
最后更新于 2023-10-17