【问题标题】:PyPDF2 write doesn't work on some PDF files (Python 3.5.1)PyPDF2 写入不适用于某些 PDF 文件(Python 3.5.1)
【发布时间】:2018-02-09 05:06:24
【问题描述】:

首先我使用的是 Python 3.5.1(32 位版本) 我编写了以下程序,使用 PyPDF2 和 reportlab 在我的 pdf 文件的所有页面上添加页码:

#import modules
from os import listdir
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
#initial values of variable declarations
PDFlist=[]
X_value=460
Y_value=820
#Make a list of al files in de directory
filelist = listdir()
#Make a list of all pdf files in the directory
for i in range(0,len(filelist)):
    filename=filelist[i]
    for j in range(0,len(filename)):
        char=filename[j]
        if char=='.':
            extension=filename[j+1:j+4]
            if extension=='pdf':
                PDFlist.append(filename)
        j=j+1
    i=i+1
# Give the horizontal position for the page number (Enter = use default value of 480)
User = input('Give horizontal position page number (ENTER = default 460): ')
if User != "":
    X_value=int(User)
# Give the vertical position for the page number (Enter = use default value of 820)
User = input('Give horizontal position page number (ENTER = default 820): ')
if User != "":
    Y_value=int(User)

for i in range(0,len(PDFlist)):
    filename=PDFlist[i]

    # read the PDF
    existing_pdf = PdfFileReader(open(filename, "rb"))
    print("File: "+filename)
    # count the number of pages
    number_of_pages = existing_pdf.getNumPages()
    print("Number of pages detected:"+str(number_of_pages))
    output = PdfFileWriter()

    for k in range(0,number_of_pages):
        packet = io.BytesIO()

        # create a new PDF with Reportlab
        can = canvas.Canvas(packet, pagesize=A4)
        Pagenumber=" Page "+str(k+1)+"/"+str(number_of_pages)
        # we first make a white rectangle to cover any existing text in the pdf
        can.setFillColorRGB(1,1,1)
        can.setStrokeColorRGB(1,1,1)
        can.rect(X_value-10,Y_value-5,120,20,fill=1)
        # set the font and size
        can.setFont("Helvetica",14)
        # choose color of page numbers (red)
        can.setFillColorRGB(1,0,0)
        can.drawString(X_value, Y_value, Pagenumber)
        can.save()
        print(Pagenumber)

        #move to the beginning of the StringIO buffer
        packet.seek(0)
        new_pdf = PdfFileReader(packet)
        # add the "watermark" (which is the new pdf) on the existing page
        page = existing_pdf.getPage(k)
        page.mergePage(new_pdf.getPage(0))
        output.addPage(page)
        k=k+1
    # finally, write "output" to a real file

    ResultPDF="Output/"+filename
    outputStream = open(ResultPDF, "wb")
    output.write(outputStream)
    outputStream.close()
    i=i+1

该程序适用于相当多的 PDF 文件(尽管有时会生成警告,如“PdfReadWarning: Superfluous whitespace found in object header b'16' b'0' [pdf.py:1666]”,但生成的输出文件对我来说没问题)。 但是,该程序无法处理某些 PDF 文件,尽管这些文件在我的 Adob​​e Acrobat 中是完全可读和可编辑的。我的印象是,错误主要出现在已扫描的 PDF 文件上,但并非全部出现(我还对未生成任何错误的扫描 PDF 文件进行了编号)。 我收到以下错误消息(前 8 行是我自己的打印命令的结果):

File: Scanned file.pdf
Number of pages detected:6
 Page 1/6
 Page 2/6
 Page 3/6
 Page 4/6
 Page 5/6
 Page 6/6
PdfReadWarning: Object 25 1 not defined. [pdf.py:1629]
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\Sourcecode\PDFPager.py", line 83, in <module>
    output.write(outputStream)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 482, in write
    self._sweepIndirectReferences(externalReferenceMap, self._root)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 577, in _sweepIndirectReferences
    newobj = data.pdf.getObject(data)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\PyPDF2\pdf.py", line 1631, in getObject
    raise utils.PdfReadError("Could not find object.")
PyPDF2.utils.PdfReadError: Could not find object.

显然,这些页面已与 reportlab 创建的 PDF 合并(参见第 6/6 页的行),但最终 PyPDF2 无法生成输出 PDF 文件(我得到一个不可读的 0 字节输出文件)。 有人可以阐明如何解决这个问题吗?我搜索了互联网,但找不到真正的答案。

【问题讨论】:

  • 我在调用同一个函数时得到了同样的错误信息。您的 PDF 可以填写吗?当我将 PDF 转换为“常规”只读 PDF 时,问题已解决。
  • 与此同时,我还找到了一种解决方法,通过pdf打印机打印pdf文件,问题就解决了。
  • 哈哈,是的,确实是等价的。
  • 我认为在合并文件之前,首先检查文件是否损坏。然后合并它们。如果文件损坏或未完全下载,则合并不会成功。
  • 文件没有损坏。我可以用 pdf 阅读器毫无问题地阅读它们。但是我无法使用 python 代码合并它们。

标签: python python-3.x pdf reportlab pypdf2


【解决方案1】:

在 pdf.py 上进行以下更改:

在 pdf 的第 1633 行。 py(这意味着取消注释 if self.strict)

    if self.strict:
        raise utils.PdfReadError("Could not find object.")

并在 pdf.py 的第 501 行进行以下更改(添加尝试,除了块)

    try:
        obj.writeToStream(stream, key)
        stream.write(b_("\nendobj\n"))
    except:
        pass

干杯。

【讨论】:

  • 酷。这个修复肯定应该被推送到master中。但是似乎 pypdf2 现在没有维护 :(
  • 这个相同的修复修复了 pypdf4 上的相同问题;我在线程上发布了指向该主题的链接,以查找那里的相关错误。 pypdf4 似乎不如 pypdf2 不活跃。
  • @Watusimoto 感谢您告诉我!我在它下面添加了一条评论。让我们希望回购所有者注意到它。
  • @bmg - 我也在相关的Github issue 上发布了这个问题,请随时在这里或那里回复,我会 X-post。我们正在寻求结合您的解决方法来解决此问题,但不确定后果。看起来一个错误只是被忽略了,一个故意的,人们会假设,有条件的未提交。您是否了解为什么_这可以解决问题以及是否会导致从文档中删除内容?
  • @mwakerman 好像有人删除了 PyPDF3 存储库...如果您有问题的内容,您可以在 PyPDF4 中打开该问题并将其也放在这里吗?
【解决方案2】:

使用“strict = false”让事情对我有用。

from PyPDF2 import PdfFileMerger

pdfs = [r'file 1.pdf', r'file 2.pdf']

merger = PdfFileMerger(strict=False)

for pdf in pdfs:
    merger.append(pdf)

merger.write(r"thanks mate.pdf")

【讨论】:

  • 嘿,是的,我只是重新运行,将其设置为 True,然后创建了文档,只是带有一堆警告。我认为它解决了未创建新文档的问题,但我的问题一定有所不同。
  • 我认为在合并文件之前,首先检查文件是否损坏。然后合并它们。如果文件损坏或未完全下载,则合并不会成功。
  • 我试图不合并整个 pdf 文件,而是合并一些页面。我仍然收到严格 = False 的错误。使用上述更改修改 pdf.py 是可行的。那么,pdf.py 从未得到纠正?
【解决方案3】:

这是我的解决方案。尝试将文件写入虚拟 ByteIO 流以检查它是否损坏。

    try:
        reader = PdfFileReader(input_file)
        print("Opening '{}', pages={}".format(file_path, reader.getNumPages()))
        # Try to write it into an dummy ByteIO stream to check whether pdf is broken
        writer = PdfFileWriter()
        writer.addPage(reader.getPage(0))
        writer.write(io.BytesIO())
    except PdfReadError:
        print("Error reading '{}".format(file_path))
        continue

    

【讨论】:

    【解决方案4】:

    我刚刚在使用 pypdf2 时遇到了同样的错误。是pdf版本的问题

    只需使用 pikepdf 包,问题就消失了。

    您可以找到文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2017-07-24
      • 2019-02-14
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多