当前位置:嗨网首页>书籍在线阅读

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 中的整数,但不包括它。