Posts: 4
Threads: 3
Joined: Dec 2024
Dec-31-2024, 11:44 AM
(This post was last modified: Dec-31-2024, 12:30 PM by Gribouillis.)
Hello
I am to Merge single page pdf files to one single pdf files..Each pdf file contains only single page.(img1.pdf,img2.pdf......so on)
that should merge in order wise like img1, img2, img3...
Here is the code what I am trying through this I am getting out 0KB pdf file..
Code:
# If you need to install, type: pip install PyPDF2
import os
import PyPDF2
from PyPDF2 import PdfReader , PdfWriter, PdfMerger
pdfFiles = [] # variable
directory = r"F:\Merge\TotalFiles"
# Loop through each file in the directory
for filename in os.listdir(directory):
print(filename)
for filename in pdfFiles: # Starting a for loop.
pdfFileObj = open(filename, 'rb') # Opens each of the file paths in filename variable.
pdfReader = PyPDF2.PdfReader(pdfFileObj) # Reads each of the files in the new varaible you've created above and stores into memory.
pageObj = pdfReader.pages[len(pdfReader.pages)] # Reads only those that are in the varaible.
PdfWriter.add_page(pageObj) # Adds each
# Name of the PDF file can be written here.
pdfOutput = open('finalFile.pdf', 'wb')
# Writing the output file using the pdfWriter function.
PdfWriter.write(pdfOutput)
pdfOutput.close() Any help can be appreciated, thank you
Posts: 4,744
Threads: 75
Joined: Jan 2018
PyPDF2 has a documentation page that explains how to merge files. Use it!
« We can solve any problem by introducing an extra level of indirection »
Posts: 9
Threads: 2
Joined: Dec 2024
(Dec-31-2024, 11:44 AM)druva Wrote: Hello
I am to Merge single page pdf files to one single pdf files..Each pdf file contains only single page.(img1.pdf,img2.pdf......so on)
that should merge in order wise like img1, img2, img3...
Here is the code what I am trying through this I am getting out 0KB pdf file..
Code:
# If you need to install, type: pip install PyPDF2
import os
import PyPDF2
from PyPDF2 import PdfReader , PdfWriter, PdfMerger
pdfFiles = [] # variable
directory = r"F:\Merge\TotalFiles"
# Loop through each file in the directory
for filename in os.listdir(directory):
print(filename)
for filename in pdfFiles: # Starting a for loop.
pdfFileObj = open(filename, 'rb') # Opens each of the file paths in filename variable.
pdfReader = PyPDF2.PdfReader(pdfFileObj) # Reads each of the files in the new varaible you've created above and stores into memory.
pageObj = pdfReader.pages[len(pdfReader.pages)] # Reads only those that are in the varaible.
PdfWriter.add_page(pageObj) # Adds each
# Name of the PDF file can be written here.
pdfOutput = open('finalFile.pdf', 'wb')
# Writing the output file using the pdfWriter function.
PdfWriter.write(pdfOutput)
pdfOutput.close() Any help can be appreciated, thank you
import os
from PyPDF2 import PdfMerger
# Directory containing PDF files
directory = r"/Users/ralf/Downloads"
# Initialize PdfMerger
merger = PdfMerger()
# Loop through each file in the directory
for filename in os.listdir(directory):
if filename.endswith(".pdf"): # Check if it's a PDF file
file_path = os.path.join(directory, filename)
print(f"Adding: {file_path}")
merger.append(file_path)
# Output file name
output_path = os.path.join(directory, "finalFile.pdf")
# Write the merged PDF to the output file
merger.write(output_path)
merger.close()
print(f"PDF files merged successfully into {output_path}")
Posts: 4,744
Threads: 75
Joined: Jan 2018
Also the PyPDF2 documentation says that this module is deprecated and that you should use pypdf.
« We can solve any problem by introducing an extra level of indirection »
Posts: 9
Threads: 2
Joined: Dec 2024
(Dec-31-2024, 12:53 PM)Gribouillis Wrote: Also the PyPDF2 documentation says that this module is deprecated and that you should use pypdf.
But it seems still working
Last login: Tue Dec 31 13:38:01 on ttys003
/var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
ralf@Rats-Mac-mini ~ % /var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
Adding: /Users/ralf/Downloads/Naming convention improvements.pdf
Adding: /Users/ralf/Downloads/finalFile.pdf
Adding: /Users/ralf/Downloads/Rechnung_001186313_handyhuellen.pdf
PDF files merged successfully into /Users/ralf/Downloads/finalFile.pdf
------------------
(program exited with code: 0)
Press return to continue
Posts: 9
Threads: 2
Joined: Dec 2024
(Dec-31-2024, 01:02 PM)ratwolf Wrote: (Dec-31-2024, 12:53 PM)Gribouillis Wrote: Also the PyPDF2 documentation says that this module is deprecated and that you should use pypdf.
But it seems still working
Last login: Tue Dec 31 13:38:01 on ttys003
/var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
ralf@Rats-Mac-mini ~ % /var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
Adding: /Users/ralf/Downloads/Naming convention improvements.pdf
Adding: /Users/ralf/Downloads/finalFile.pdf
Adding: /Users/ralf/Downloads/Rechnung_001186313_handyhuellen.pdf
PDF files merged successfully into /Users/ralf/Downloads/finalFile.pdf
------------------
(program exited with code: 0)
Press return to continue
When using pypdf you have to replace PdfMerger with PdfWriter
import os
from pypdf import PdfWriter
# Directory containing PDF files
directory = r"/Users/ralf/Downloads"
# Initialize PdfMerger
merger = PdfWriter()
# Loop through each file in the directory
for filename in os.listdir(directory):
if filename.endswith(".pdf"): # Check if it's a PDF file
file_path = os.path.join(directory, filename)
print(f"Adding: {file_path}")
merger.append(file_path)
# Output file name
output_path = os.path.join(directory, "finalFile.pdf")
# Write the merged PDF to the output file
merger.write(output_path)
merger.close()
print(f"PDF files merged successfully into {output_path}")
Gribouillis likes this post
Posts: 4
Threads: 3
Joined: Dec 2024
(Dec-31-2024, 03:48 PM)ratwolf Wrote: (Dec-31-2024, 01:02 PM)ratwolf Wrote: But it seems still working
Last login: Tue Dec 31 13:38:01 on ttys003
/var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
ralf@Rats-Mac-mini ~ % /var/folders/1w/f0d5lrpn2cq3v857fgrfzt240000gp/T/geany_run_script_VFRTZ2.sh ; exit;
Adding: /Users/ralf/Downloads/Naming convention improvements.pdf
Adding: /Users/ralf/Downloads/finalFile.pdf
Adding: /Users/ralf/Downloads/Rechnung_001186313_handyhuellen.pdf
PDF files merged successfully into /Users/ralf/Downloads/finalFile.pdf
------------------
(program exited with code: 0)
Press return to continue
When using pypdf you have to replace PdfMerger with PdfWriter
import os
from pypdf import PdfWriter
# Directory containing PDF files
directory = r"/Users/ralf/Downloads"
# Initialize PdfMerger
merger = PdfWriter()
# Loop through each file in the directory
for filename in os.listdir(directory):
if filename.endswith(".pdf"): # Check if it's a PDF file
file_path = os.path.join(directory, filename)
print(f"Adding: {file_path}")
merger.append(file_path)
# Output file name
output_path = os.path.join(directory, "finalFile.pdf")
# Write the merged PDF to the output file
merger.write(output_path)
merger.close()
print(f"PDF files merged successfully into {output_path}")
Thanks the code is working as expected ,but i was to sort by file name merging should happen with filename img1 ,img2 ..
Posts: 9
Threads: 2
Joined: Dec 2024
Jan-01-2025, 07:43 AM
(This post was last modified: Jan-01-2025, 07:43 AM by ratwolf.)
(Jan-01-2025, 05:20 AM)druva Wrote: (Dec-31-2024, 03:48 PM)ratwolf Wrote: When using pypdf you have to replace PdfMerger with PdfWriter
import os
from pypdf import PdfWriter
# Directory containing PDF files
directory = r"/Users/ralf/Downloads"
# Initialize PdfMerger
merger = PdfWriter()
# Loop through each file in the directory
for filename in os.listdir(directory):
if filename.endswith(".pdf"): # Check if it's a PDF file
file_path = os.path.join(directory, filename)
print(f"Adding: {file_path}")
merger.append(file_path)
# Output file name
output_path = os.path.join(directory, "finalFile.pdf")
# Write the merged PDF to the output file
merger.write(output_path)
merger.close()
print(f"PDF files merged successfully into {output_path}")
Thanks the code is working as expected ,but i was to sort by file name merging should happen with filename img1 ,img2 ..
import os
from pypdf import PdfWriter
# Directory containing PDF files
directory = r"/Users/ralf/Downloads"
# Initialize PdfWriter
merger = PdfWriter()
# Get and sort the list of PDF files in the directory (case-insensitive)
pdf_files = sorted([f for f in os.listdir(directory) if f.endswith(".pdf")], key=str.lower)
# Loop through each sorted PDF file
for filename in pdf_files:
file_path = os.path.join(directory, filename)
print(f"Adding: {file_path}")
merger.append(file_path)
# Output file name
output_path = os.path.join(directory, "finalFile.pdf")
# Write the merged PDF to the output file
merger.write(output_path)
merger.close()
print(f"PDF files merged successfully into {output_path}") This should do it in an alphabetic order
key=str.lower shall ensure that upper cases are not proritized
Documents starting with numbers will come first
|