W14 <<
Previous Next >> W16-W17
W15
利用Pypdf2切割pdf檔
切割PDF之.py程式檔
# pypdf2_ex1.py
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
pdfReader = PdfFileReader(open("2002_Book_Real-WorldASPNETBuildingAConte.pdf", "rb"))
information = [("WhatIsCMS",18,45)]
for page in range(len(information)):
pdf_writer = PdfFileWriter()
start = information[page][1]
end = information[page][2]
while start<=end:
pdf_writer.addPage(pdfReader.getPage(start-1))
start+=1
if not os.path.exists("./"):
os.makedirs(savepath)
output_filename = '{}_{}_page_{}.pdf'.format(information[page][0],information[page][1], information[page][2])
with open(output_filename,'wb') as out:
pdf_writer.write(out)