未找到EOF标记 - 如何修复PyPDF和PyPDF2?

时间:2013-04-23 06:30:15

标签: python pdf pypdf

我正在尝试使用Python将一些PDF文件合并到一个PDF文件中。我已经尝试了PyPDF和PyPDF2 - 在某些文件中,它们都抛出同样的错误:

PdfReadError:未找到EOF标记

这是我的代码(page_files)是要合并的PDF文件路径列表:

# use pypdf to combine pdf pages
output = PdfFileWriter()
for pf in page_files:
    filestream = file(pf, "rb")
    pdf = PdfFileReader(filestream)            
    for num in range(pdf.getNumPages()):
        output.addPage(pdf.getPage(num))            

# write final file
outputStream = file(pdf_full_path, "wb")
output.write(outputStream)
outputStream.close()

我已经阅读了关于该主题的一些StackOverflow线程,但没有一个包含有效的解决方案。如果您已经使用Python成功组合了PDF文件,我很乐意听到。谢谢!

1 个答案:

答案 0 :(得分:0)

是否仍然有人希望合并pdf的“列表”:

注意: 使用glob获取正确的文件列表。 <-这将确保您的一天安全^^

查看以下内容:glob module reference

from functools import partial
from dataclasses import dataclass, field
import sys
from typing import List, Callable


@dataclass
class Observable:
    observers: List[Callable] = field(default_factory=list)

    def register(self, observer: Callable):
        self.observers.append(observer)

    def deregister(self, observer: Callable):
        self.observers.remove(observer)

    def notify(self, *args, **kwargs):
        for observer in self.observers:
            observer(*args, **kwargs)


def usage_demo():
    observable = Observable()

    # Register two anonymous observers using lambda.
    observable.register(
        lambda *args, **kwargs: print(f'Observer 1 called with args={args}, kwargs={kwargs}'))
    observable.register(
        lambda *args, **kwargs: print(f'Observer 2 called with args={args}, kwargs={kwargs}'))

    # Create an observer function, register it, then deregister it.
    def callable_3():
        print('Observer 3 NOT called.')

    observable.register(callable_3)
    observable.deregister(callable_3)

    # Create a general purpose observer function and register four observers.
    def callable_x(*args, **kwargs):
        print(f'{args[0]} observer called with args={args}, kwargs={kwargs}')

    for gui_field in ['Form field 4', 'Form field 5', 'Form field 6', 'Form field 7']:
        observable.register(partial(callable_x, gui_field))

    observable.notify('test')


if __name__ == '__main__':
    sys.exit(usage_demo())