PYPDF2给出“ TypeError:参数应该是整数或无,而不是NullObject;
使用此脚本,我将创建一个结合了许多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.argv 索引对我来说。
这里的文件链接在此代码中效果很好:
“引起此问题的文件:
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'
With this script I'm going to create one single pdf file that combines the many pdfs in the
folder that I gave it in this line of code input_folder_pdf = sys.argv[1]
from the terminal and it creates the output folder if not exists .
this code worked
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!')
I ran this code like this in the porwershell :python3.9.exe {this_script.py} {.\the_path_folder_of_the_pdfs\} {.\the_output_folder\}
giving the folder name with spaces cause a problem with sys.argv1 index for me.
here link of files that works good with this code:
folder of pdfs that works well with this code
here is the link of files that cause this problem:
pdf1
pdf2
this error happens :
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从PYPDF2移至PYPDF。我是两个项目的维护者,我不再更新PYPDF2。
该错误仍然存在于
pypdf == 3.2.0
中,但不再在pypdf == 3.4.1
中。 升级您的pypdf
版本。如何提出好问题
作为旁注,表明该问题的代码的问题部分可能会降低到:
带有Python 3.11的追溯将看起来像这样:
Move from PyPDF2 to pypdf. I'm the maintainer of both projects and I no longer update PyPDF2.
The bug was still present in
pypdf==3.2.0
but is no longer inpypdf==3.4.1
. Upgrade yourpypdf
version.How to ask good questions
As a side-note, the problematic parts of the code that showed the issue could have been reduced to:
A traceback with Python 3.11 would then look like this: