【问题标题】:KeyError: '/Root' in PyPDF2KeyError:PyPDF2 中的 \'/Root\'
【发布时间】:2023-01-04 06:07:15
【问题描述】:

我尝试附加一个 PDF

from PyPDF2 import PdfMerger

attachment = 'C:/Users/John Doe/Desktop/test.pdf'

merger = PdfMerger()

merger.append(attachment)

它给了我这个错误。

"C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\Joe Medina\Desktop\Joseph\Geoscope\Binders\tests.py"
incorrect startxref pointer(1)
Traceback (most recent call last):
  File "C:\Users\Joe Medina\Desktop\Joseph\Geoscope\Binders\tests.py", line 10, in <module>
    merger.append(attachment)
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_utils.py", line 390, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_merger.py", line 283, in append
    self.merge(len(self.pages), fileobj, outline_item, pages, import_outline)
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_utils.py", line 390, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_merger.py", line 174, in merge
    pages = (0, len(reader.pages))
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_page.py", line 2033, in __len__
    return self.length_function()
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_reader.py", line 446, in _get_num_pages
    self._flatten()
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_reader.py", line 1074, in _flatten
    catalog = self.trailer[TK.ROOT].get_object()
  File "C:\Users\Joe Medina\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\generic\_data_structures.py", line 149, in __getitem__
    return dict.__getitem__(self, key).get_object()
KeyError: '/Root'

Process finished with exit code 1

我可以合并所有 PDF,除了一些特殊情况,比如这个。

奇怪的是,当我将 PDF“另存为”成同名的 PDF 时,程序运行正常。当我保存它们时,PDF 版本是一样的。

知道是什么原因造成的吗?

【问题讨论】:

  • 听起来像是 PyPDF2 错误。您是否尝试过提交错误报告?
  • 还没有。我再等几天再提交报告。

标签: python-3.x pypdf


【解决方案1】:

我是 pypdf 和 PyPDF2 的维护者(只有 pypdf 还在维护)。

这对我来说听起来像是一个错误。请提交错误报告:https://github.com/py-pdf/pypdf/issues

一个好的错误报告包含:

  1. 您的代码
  2. 所有必需的文件
  3. 包的所有版本(请先移至pypdf)

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2017-10-20
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多