- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我使用下面的代码时
from PyPDF2 import PdfFileMerger
merge = PdfFileMerger()
for newFile in nlst:
merge.append(newFile)
merge.write("newFile.pdf")
发生了如下事情:
raise utils.PdfReadError("EOF marker not found")
PyPDF2.utils.PdfReadError: EOF marker not found
谁能告诉我发生了什么事?
最佳答案
在使用 camelot
和 PyPDF2
遇到这个问题后,我做了一些挖掘并解决了这个问题。
文件结束标记 '%%EOF'
本来是最后一行,但有些 PDF 文件在这一行之后放了一大块 javascript,读者找不到 EOF .
打开 EOF 和 javascript 后的样子:
b'>>\r\n',
b'startxref\r\n',
b'275824\r\n',
b'%%EOF\r\n',
b'\n',
b'\n',
b'<script type="text/javascript">\n',
b'\twindow.parent.focus();\n',
b'</script><!DOCTYPE html>\n',
b'\n',
b'\n',
b'\n',
所以你只需要在 javascript 开始之前截断文件。
解决方法:
def reset_eof_of_pdf_return_stream(pdf_stream_in:list):
# find the line position of the EOF
for i, x in enumerate(txt[::-1]):
if b'%%EOF' in x:
actual_line = len(pdf_stream_in)-i
print(f'EOF found at line position {-i} = actual {actual_line}, with value {x}')
break
# return the list up to that point
return pdf_stream_in[:actual_line]
# opens the file for reading
with open('data/XXX.pdf', 'rb') as p:
txt = (p.readlines())
# get the new list terminating correctly
txtx = reset_eof_of_pdf_return_stream(txt)
# write to new pdf
with open('data/XXX_fixed.pdf', 'wb' as f:
f.writelines(txtx)
fixed_pdf = PyPDF2.PdfFileReader('data/XXX_fixed.pdf')
关于python - 在 python 中使用 PyPDF2 合并 pdf 文件时找不到 EOF 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45390608/
我尝试安装这个包... $ pip search pyPdf PyPDFLite - Simple PDF Writer. pypdfocr
使用它时出现意外错误。第一部分来自我在网上找到的脚本,我试图用它来提取 PDF 大纲中标识的特定部分。一切正常,除了在 output.write(outputfile1)它说: PdfReadErro
Here我找到了用于拆分 pdf 页面的代码。 #!/usr/bin/env python import copy, sys from pyPdf import PdfFileWriter, PdfF
使用 pypdf python 模块如何读取以下 pdf 文件 http://www.envis-icpe.com/pointcounterpointbook/Hindi_Book.pdf # -*-
我是Python新手。我尝试打开pdf文件并将其内容写入新的文本文件。文本文件名称由 pdf 名称生成。到目前为止我已经尝试过,但它没有达到我的预期。我怎样才能实现它 import glob,
我实际上是在使用 pyPdf 打开、读取和写入 PDF 文件的内容。 为此,我使用了这些代码行: from pyPdf import PdfFileWriter, PdfFileReader pdf
按照这个例子,我可以将所有元素列成一个pdf文件 import pyPdf pdf = pyPdf.PdfFileReader(open("pdffile.pdf")) list(pdf.pages)
如果我有 1000 多个 pdf 文件需要合并为一个 pdf, from PyPDF2 import PdfReader, PdfWriter writer = PdfWriter() for i i
我有一个 PDF 文件(A4,纵向布局),我想将其中的每一页分成一半高度。输出文件也应该是A4和纵向布局,但每页的下半部分需要是空白的。 我看到了https://stackoverflow.com/a
我有一个 PDF 文件(A4,纵向布局),我想将其中的每一页分成一半高度。输出文件也应该是A4和纵向布局,但每页的下半部分需要是空白的。 我看到了https://stackoverflow.com/a
我正在运行以下代码来创建一个新的 PDF 文件,其中包含源 PDF 的除第一页之外的所有页面: import os from pyPdf import PdfFileReader, PdfFileWr
这个问题在这里已经有了答案: How can I rotate a page with PyPDF2? (2 个答案) 关闭 12 个月前。 我们有一个简单的脚本来读取传入的 PDF 文件。如果是横
目前,如果我使用 pyPdf 和 extractText() 创建一个 pdf 页面的页面对象,会发生什么行被连接在一起。例如,如果页面的第 1 行说“hello”而第 2 行说“world”,则从
目前我正在研究将 PDF 与 pyPdf 合并,但有时输入的顺序不正确,所以我正在研究抓取每一页的页码以确定它应该进入的顺序(例如,如果有人将一本书分成 20 份 10 页的 PDF,而我想将它们重新
我在 Windows 平台上使用 Python 2.4 和 PyPdf 1.13。我正在尝试使用以下代码将列表中的 PDF 文件合并为一个文件: import os from pyPdf import
我想将几个 PDF 文件合并为一个 PDF 文档。事实证明,输入文件并不完全符合标准。 EOF 标记后跟一些附加信息: >> startxref 1994481 %%EOF %%PPIRoute: 4
我有以下代码: import os from pyPdf import PdfFileReader, PdfFileWriter path = "C:/Real Python/Course mater
我正在尝试将 PDF 的每一页提取为字符串: import pyPdf pages = [] pdf = pyPdf.PdfFileReader(file('g-reg-101.pdf', 'rb')
我正在尝试使用 pyPdf 从多页 PDF 中提取和打印页面。问题是,文本不是从某些页面中提取的。我在这里放了一个示例文件: http://www.4shared.com/document/kmJF6
我正在尝试使用 pyPDF 编写几个 python 脚本,将 PDF 页面拆分为六个单独的页面,正确排序它们(通常正面和背面打印,因此每个其他页面都需要以不同方式排序),并删除结果输出文档末尾的空白页
我是一名优秀的程序员,十分优秀!