PDF Merge – With GUI (Python)

Yesterday I looked at using the tkinter module of Python to create a file browser GUI , and it has inspired me to relook at the PDF Merge program I did a while back.

My first attempt (worked on over a lunch break) at a GUI driven PDF merge program is below, I’ve tested it and it works but I still feel there is work to be done to a) make it look nicer and b) give the user more information.


#!/usr/bin/python3
# geektechstuff

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

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

# Ask user where the PDFs are
first_pdf_to_merge = filedialog.askopenfilename()
root.withdraw()
root.update()


second_pdf_to_merge = filedialog.askopenfilename()
root.withdraw()
root.update()


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

# Get all the PDF filenames
pdf2merge = [first_pdf_to_merge, second_pdf_to_merge]


pdfWriter = PyPDF2.PdfFileWriter()

# 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')

Screen Shot 2018-07-03 at 12.48.21Screen Shot 2018-07-03 at 12.48.30Screen Shot 2018-07-03 at 12.49.02

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

  1. PDF Merge – With GUI V2 (Python) – Geek Tech Stuff Avatar

    […] 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 […]

    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