python 没有找到EOF标记-如何在PyPDF和PyPDF 2中修复?

huus2vyu  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(123)

我尝试使用Python将几个PDF文件合并成一个PDF文件。我已经尝试了PyPDF和PyPDF 2-对于一些文件,它们都抛出了相同的错误:
Pdf读取错误:找不到EOF标记
下面是我的代码(page_files),它是一个要合并的PDF文件路径列表:

from PyPDF2 import PdfReader, PdfWriter

writer = PdfWriter()
for path in ["example1.pdf", "example2.pdf"]:
    reader = PdfReader(path)            
    for page in reader.pages:
        writer.add_page(page)            

with open("out.pdf", "wb") as fp:
    writer.write(fp)

我读过一些关于这个主题的StackOverflow线程,但是没有一个包含有效的解决方案。如果你已经成功地使用Python合并了PDF文件,我很想听听你是怎么做的。

0x6upsns

0x6upsns1#

您运行的PyPDF2出现问题,该问题已通过PR #321解决。该修复程序已在PyPDF2==1.27.8中发布(发布日期:2022年4月21日)。

piok6c0g

piok6c0g2#

是否仍有人在寻找合并PDF的“列表”:
注意:使用glob获取正确的文件列表。〈-这将真正保护您的一天^^
看看这个:glob模块引用

from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
import os
import glob

class MergeAllPDF:
    def __init__(self):
        self.mergelist = []

    def create(self, filepath, outpath, outfilename):
        self.outfilname = outfilename
        self.filepath = filepath
        self.outpath = outpath
        self.pdfs = glob.glob(self.filepath)
        self.myrange = len(self.pdfs)

        for _ in range(self.myrange):
            if self.pdfs:
                self.mergelist.append(self.pdfs.pop(0))
        self.merge()

    def merge(self):
        if self.mergelist:
            self.merger = PdfFileMerger()
            for pdf in self.mergelist:
                self.merger.append(open(pdf, 'rb'))  
            self.merger.write(self.outpath + "%s.pdf" % (self.outfilname))
            self.merger.close()
            self.mergelist = []
        else:
            print("mergelist is empty please check your input path")

# example how to use
#update your path here:

inpath = r"C:\Users\Fabian\Desktop\mergeallpdfs\scan\*.pdf" #here are your single page pdfs stored
outpath = r"C:\Users\Fabian\Desktop\mergeallpdfs\output\\" #here your merged pdf will be stored

b = MergeAllPDF()
b.create(inpath, outpath, "mergedpdf")

相关问题