W15 <<
Previous Next >> 操作內容教學
W16-W17
利用pypdf2擷取pdf與合併pdf的程式,並利用API放上動態網站測試。
利用pypdf2擷取出3個pdf檔
# pypdf2_ex2.py
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
pdfReader = PdfFileReader(open("2019_Book_PracticalFlutter.pdf", "rb"))
information = [("GitAndGithub",16,26),("GitAndGithub",36,46),("GitAndGithub",196,289)]
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)
再使用以下程式合併成一個pdf檔
# pypdf2_ex3.py
# merge several pdf files
import sys
import PyPDF2
pdf_write_object = PyPDF2.PdfFileWriter()
#for i in sys.argv[1:]:
for i in ["GitAndGithub_16_page_26.pdf","GitAndGithub_36_page_46.pdf","GitAndGithub_196_page_289.pdf"]:
pdf_read_object = PyPDF2.PdfFileReader(i)
print('processing file : '+ i + 'number of pages : ' + str(pdf_read_object.numPages))
for page in range(pdf_read_object.numPages):
pdf_write_object.addPage(pdf_read_object.getPage(page))
final_file_object = open('2020CMS.pdf', 'wb')
pdf_write_object.write(final_file_object)
final_file_object.close()
Google API
W15 <<
Previous Next >> 操作內容教學