10-第4步_保存结果
第4步:保存结果
在这些嵌套的 for
循环完成后, pdfWriter
变量将包含一个 PdfFileWriter
对象,以合并所有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):
--snip--
# Save the resulting PDF to a file.
pdfOutput = open('allminutes.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()
向 open()
传入 'wb'
,以写二进制的模式打开PDF文档allminutes.pdf。然后,将得到的 File
对象传给 write()
方法,以创建实际的PDF文档。调用 close()
方法,结束程序。