Skip to content

Instantly share code, notes, and snippets.

@jacobsalmela
Last active November 21, 2020 22:44
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacobsalmela/c1d9a654fbe6a4c12775e68ea1abb95a to your computer and use it in GitHub Desktop.
Save jacobsalmela/c1d9a654fbe6a4c12775e68ea1abb95a to your computer and use it in GitHub Desktop.
Merge PDFs using Python
#!/usr/bin/env python
# Jacob Salmela
# Make PyPDF2 is installed: sudo easy_install PyPDF2
# https://jacobsalmela.com/2016/08/12/merge-pdfs-natively-with-a-right-click-in-os-x/
import sys
import os
from PyPDF2 import PdfFileMerger, PdfFileReader
merger = PdfFileMerger()
# Get the folder of the first file and that's where the merged PDF will go
dirname = os.path.dirname(sys.argv[1])
for f in sys.argv[1:]:
filename = os.path.basename(f)
print("Appending " + filename + "...")
# Append each page to the merger
merger.append(PdfFileReader(file(f, 'rb')))
# Close the file to prevent duplicate pages from being appended
file(f).close()
# Write all the appends to a new file
merger.write(os.path.join(dirname, "merged.pdf"))
@znuc89
Copy link

znuc89 commented Mar 14, 2017

-: line 3: import: command not found
-: line 4: import: command not found
from: can't read /var/mail/PyPDF2
-: -c: line 7: syntax error near unexpected token (' -: -c: line 7: merger = PdfFileMerger()'

How to fix it?

@DeFilippis
Copy link

I'm getting an Import Error with PyPDF2, which I suspect is likely due to me having Python3 installed. Anybody know how to fix this? The usual methods did not work. (tried pip install, pip3 install, conda install, brew install)

@jacobsalmela
Copy link
Author

Hmm, it's been so long since I even wrote this...

@Lovepankie
Copy link

The code runs but i cant seem to find the location of my merged pdf. Can someone here help me please?

@jacobsalmela
Copy link
Author

jacobsalmela commented May 23, 2020

If you remove the get selected finder items from the workflow, those duplicates don't happen anymore. I updated my blog post to reflect that.

@Lovepankie
Copy link

Thank you @jacobsalmela. I finally figured it out and then created a GUI version from your code. Thank you so much

@jacobsalmela
Copy link
Author

NP. Glad it's still useful. I've been going through some of my old posts to fixup stuff like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment