PDF Merge – With GUI V2 (Python)

A small change to yesterday’s PDF Merge with GUI. The below now asks the user how many PDFs they want to merge, and then loops through the process until that many PDFs are merged into one PDF.

#!/usr/bin/python3
# geektechstuff

# libraries to import
from tkinter import *
from tkinter import filedialog
import PyPDF2

# defining variables
pdf2merge=[]
pdfWriter = PyPDF2.PdfFileWriter()

# creates Tk window
root = Tk()
root.update()
root.withdraw()

# asks user how many PDFs to merge
number_to_merge = input('How may files to merge?: ')
number_to_merge = int(number_to_merge)

# asks users where the PDFs are
for x in range(number_to_merge):
    pdf_to_merge = filedialog.askopenfilename()
    root.withdraw()
    root.update()
    pdf2merge.append(pdf_to_merge)

# Ask user for the name to save the file as
userfilename = filedialog.asksaveasfilename()

# loop through all PDFs
for filename in pdf2merge:
    # rb for read binary
    pdfFileObj = open(filename, 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    # Opening each page of the PDF
    for pageNum in range(pdfReader.numPages):
        pageObj = pdfReader.getPage(pageNum)
        pdfWriter.addPage(pageObj)
# save PDF to file, wb for write binary
pdfOutput = open(userfilename + '.pdf', 'wb')
# Outputting the PDF
pdfWriter.write(pdfOutput)
# Closing the PDF writer
pdfOutput.close()
# closes Tk window
root.destroy()

print('Finished')

PDF Merge GUI V2

One response to “PDF Merge – With GUI V2 (Python)”

  1. PDF Merge Website (Python) – Geek Tech Stuff Avatar

    […] Merging multiple PDFs seems to be a common request, and most people don’t want to spend a small fortune on a tool to do the merging. I first looked at this problem with an early Python project (https://geektechstuff.com/2018/02/17/python-3-merge-multiple-pdfs-into-one-pdf/) and later improved on that project by adding a simple GUI (https://geektechstuff.com/2018/07/04/pdf-merge-with-gui-v2-python/). […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect