windows PyPDF2给出“类型错误:参数应为整数或None,而不是“NullObject”

xggvc2p6  于 2023-03-04  发布在  Windows
关注(0)|答案(1)|浏览(179)

使用此脚本,我将创建一个单独的pdf文件,该文件组合了文件夹中的许多pdf文件,我在此代码行input_folder_pdf = sys.argv[1]中从终端提供了该文件夹,如果不存在,它将创建输出文件夹。此代码工作正常

import PyPDF2
import sys
import os
input_folder_pdf = sys.argv[1]
output_folder_file = sys.argv[2]
if not os.path.exists(output_folder_file):
    os.makedirs(output_folder_file)
    print(output_folder_file, 'folder crated!')
input_name = input('name the combined pdf : ')
pdf_inputs = []
for filename in os.listdir(input_folder_pdf):
    name = f'{input_folder_pdf}{filename}'
    pdf_inputs.append(name)
merger = PyPDF2.PdfFileMerger(strict=False)
for pdf in pdf_inputs:
    merger.append(pdf)
    print(pdf, ' added!!')
combined_name = str(output_folder_file) + str(input_name) + '.pdf'
merger.write(combined_name)
print('Done!')

我在porwershell中运行了如下代码:python3.9.exe {this_script.py} {.\the_path_folder_of_the_pdfs\} {.\the_output_folder\}给文件夹名加上空格会导致sys.argv1索引出现问题。
下面是可以很好地使用此代码的文件的链接:folder of pdfs that works well with this code
以下是导致此问题的文件的链接:pdf1pdf2发生此错误:

C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\_reader.py:993: PdfReadWarning: Invalid stream (index 35) within object 96 0: Stream has ended unexpectedly
  warnings.warn(
Traceback (most recent call last):
  File "F:\download telegram desktop\Manga\jujustu_kaisen\Pdf_combiner.py", line 22, in <module>
    merger.write(final_name)
  File"C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\PyPDF2\generic.py", line 800, in read_from_stream
    data["__streamdata__"] = stream.read(length)
TypeError: argument should be integer or None, not 'NullObject'
q8l4jmvw

q8l4jmvw1#

从PyPDF2移到pypdf。我是这两个项目的维护者,我不再更新PyPDF2。
pypdf==3.2.0中仍然存在该错误,但pypdf==3.4.1中已不再存在。升级您的pypdf版本

如何问好问题

作为旁注,代码中显示问题的有问题部分可以减少到:

from pypdf import PdfMerger

merger = PdfMerger(strict=False)
merger.append("problematic-doc.pdf")
merger.write("out-combined.pdf")

使用Python 3.11的回溯将如下所示:

Traceback (most recent call last):
  File "/home/moose/so-pypdf/foo.py", line 4, in <module>
    merger.append("problematic-doc.pdf")
  File "/home/moose/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pypdf/_utils.py", line 441, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pypdf/_merger.py", line 317, in append
    self.merge(len(self.pages), fileobj, outline_item, pages, import_outline)
  File "/home/moose/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pypdf/_utils.py", line 441, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pypdf/_merger.py", line 194, in merge
    stream, encryption_obj = self._create_stream(fileobj)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/moose/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pypdf/_merger.py", line 263, in _create_stream
    stream = FileIO(fileobj, "rb")
             ^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'problematic-doc.pdf'

相关问题