- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码:
import os
from pyPdf import PdfFileReader, PdfFileWriter
path = "C:/Real Python/Course materials/Chapter 12/Practice files"
input_file_name = os.path.join(path, "Pride and Prejudice.pdf")
input_file = PdfFileReader(file(input_file_name, "rb"))
output_PDF = PdfFileWriter()
for page_num in range(1, 4):
output_PDF.addPage(input_file.getPage(page_num))
output_file_name = os.path.join(path, "Output/portion.pdf")
output_file = file(output_file_name, "wb")
output_PDF.write(output_file)
output_file.close()
到目前为止,我只是从 Pdfs 中阅读,后来学会了从 Pdf 写入 txt...但是现在这个...为什么 PdfFileReader
与 PdfFileWriter
有人能解释一下吗?我期望类似的东西:
import os
from pyPdf import PdfFileReader, PdfFileWriter
path = "C:/Real Python/Course materials/Chapter 12/Practice files"
input_file_name = os.path.join(path, "Pride and Prejudice.pdf")
input_file = PdfFileReader(file(input_file_name, "rb"))
output_file_name = os.path.join(path, "out Pride and Prejudice.pdf")
output_file = PdfFileWriter(file(output_file_name, "wb"))
for page_num in range(1,4):
page = input_file.petPage(page_num)
output_file.addPage(page_num)
output_file.write(page)
有什么帮助吗???谢谢
编辑0: .addPage() 的作用是什么?
for page_num in range(1, 4):
output_PDF.addPage(input_file.getPage(page_num))
它只是创建 3 个空白页吗?
编辑1:有人可以解释一下发生了什么:
1) output_PDF = PdfFileWriter()
2) output_PDF.addPage(input_file.getPage(page_num))
3) output_PDF.write(output_file)
第三个将 JUST CREATED(!) 对象传递给 output_PDF
,为什么?
最佳答案
问题基本上是 PDF 交叉引用表。
这是一个有点困惑的意大利面条怪物,引用了页面、字体、对象、元素,而这些都需要链接在一起以允许随机访问。
每次更新文件时,都需要重建该表。该文件首先在内存中创建,因此这只需要发生一次,并进一步减少文件被烧毁的机会。
output_PDF = PdfFileWriter()
这会在内存中创建供 PDF 进入的空间。 (从您的旧 pdf 中提取)
output_PDF.addPage(input_file.getPage(page_num))
将输入 pdf 中的页面添加到内存中创建的 PDF 文件中(您想要的页面。)
output_PDF.write(output_file)
最后,这会将内存中存储的对象写入文件,构建 header 、交叉引用表,并将所有内容链接在一起。
编辑:据推测,JUST CREATED 标志指示 PyPDF 开始构建适当的表并将事物链接在一起。
--
回应 .txt 和 csv 的原因:
当您从文本或 CSV 文件进行复制时,没有任何现有数据结构可供理解和移动,以确保正确保留和创建格式、图像位置和表单数据(输入部分等)等内容。
关于python - pyPdf PdfFileReader 与 PdfFileWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573256/
我打开了很多 PDF,我想在解析后删除这些 PDF,但这些文件在程序运行完成之前一直保持打开状态。如何关闭使用 PyPDF2 打开的 PDF? 代码: def getPDFContent(path):
这应该是一个非常简单的问题,我无法通过谷歌搜索找到答案:How to close file handle opened by pyPDF "PdfFileReader"Class 这是片段: impo
我有以下代码: import os from pyPdf import PdfFileReader, PdfFileWriter path = "C:/Real Python/Course mater
所以这是我的导入: from pyPdf import PdfFileWriter, PdfFileReader 这是我写的 pdf: filenamer = filename + '.pdf' pd
我有很多 pdf 页面,我想将它们合并到一个文件中。 我的脚本如下: from PyPDF2 import PdfFileMerger,PdfFileReader filename_list=[] m
我是一名优秀的程序员,十分优秀!