Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge PDFs using Python
#!/usr/bin/env python
# Jacob Salmela
# Make PyPDF2 is installed: sudo easy_install PyPDF2
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(dirname + "/merged.pdf")
@nickv2002

This comment has been minimized.

Show comment
Hide comment
@nickv2002

nickv2002 Aug 23, 2016

`file(f).close() was causing a crash so I took it out in my version. I also made some modifications to improve path handling:

#!/usr/bin/env python
# coding=utf-8

# Jacob Palmela
# Make PyPDF2 is installed: sudo easy_install PyPDF2
# Automator action: http://jacobsalmela.com/merge-pdfs-natively-with-a-right-click-in-os-x/
import sys, 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')))

# Write all the appends to a new file
merger.write(os.path.join(dirname, "merged.pdf"))

nickv2002 commented Aug 23, 2016

`file(f).close() was causing a crash so I took it out in my version. I also made some modifications to improve path handling:

#!/usr/bin/env python
# coding=utf-8

# Jacob Palmela
# Make PyPDF2 is installed: sudo easy_install PyPDF2
# Automator action: http://jacobsalmela.com/merge-pdfs-natively-with-a-right-click-in-os-x/
import sys, 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')))

# Write all the appends to a new file
merger.write(os.path.join(dirname, "merged.pdf"))

@felix822

This comment has been minimized.

Show comment
Hide comment
@felix822

felix822 Aug 25, 2016

Did you guys ever figure out the problem with pages being doubled up if original PDFs have more than one page? I'm combining 1 & 3 pages, and getting 8 total in merged.pdf

felix822 commented Aug 25, 2016

Did you guys ever figure out the problem with pages being doubled up if original PDFs have more than one page? I'm combining 1 & 3 pages, and getting 8 total in merged.pdf

@znuc89

This comment has been minimized.

Show comment
Hide comment
@znuc89

znuc89 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?

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?

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