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'