python-3.x 关键字错误:PyPDF 2中的“/Root”

yftpprvb  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(354)

我试着附加一个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版本在我保存时是一样的。
知道是什么引起的吗?

xfb7svmp

xfb7svmp1#

我是pypdf和PyPDF2的维护者(只有pypdf还在维护)。
这听起来像是一个错误。请提交错误报告:https://github.com/py-pdf/pypdf/issues
一个好的错误报告包含:
1.您的代码
1.所有必要文件
1.软件包的所有版本(请先移动到pypdf)

相关问题