Python Forum
How to Merge Single page pdf files to one pdf file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Merge Single page pdf files to one pdf file
#1
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
Gribouillis write Dec-31-2024, 12:30 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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 »
Reply
#3
(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}")
Reply
#4
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 »
Reply
#5
(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
Reply
#6
(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
Reply
#7
(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 ..
Reply
#8
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON file with links for each US state Open Data Page Larz60+ 5 6,892 Mar-05-2017, 11:19 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020