09-第3步_添加每一页
第3步:添加每一页
针对每个PDF文档,程序需要循环遍历每一页,第一页除外。在程序中添加以下代码:
#! python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2, os
--snip--
# Loop through all the PDF files.
for filename in pdfFiles:
--snip--
# Loop through all the pages (except the first) and add them.
❶ for pageNum in range(1, pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# TODO: Save the resulting PDF to a file.
for
循环内的代码将每个 Page
对象复制到 PdfFileWriter
对象。要记住,你需要跳过第一页。因为 PyPDF2
认为 0
是第一页,所以循环应该从 1
开始❶,然后向上增长到 pdfReader. umPages
中的整数,但不包括它。