Last active
January 12, 2017 22:48
-
-
Save tombasche/ec3e2a76d8612aa3dd7f843a9ee5841e to your computer and use it in GitHub Desktop.
Merges all pdf files in a directory (needs pypdf - get using pip). Instructions: pip install pillow
pip install pypdf Backup any images first
Ensure any mergedpdf files are deleted
run python merge.py in the directory with the images/pdfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import copy | |
from PIL import Image | |
from pyPdf import PdfFileWriter, PdfFileReader | |
def append_pdf(input,output): | |
[output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)] | |
output = PdfFileWriter() | |
cwd = os.getcwd() | |
for root, dirs, files in os.walk(cwd): | |
for file in files: | |
if file.endswith(".pdf"): | |
try: | |
append_pdf(PdfFileReader(open(file,"rb")),output) | |
except: | |
print "something went wrong with file: ",file | |
elif file.endswith(".jpg") or file.endswith(".jpeg"): | |
im = Image.open(file) | |
temp = copy.deepcopy(file) #stop the script from mangling the source jpg | |
outfile = temp | |
im.save(outfile, "PDF", resolution=100.0) | |
append_pdf(PdfFileReader(open(outfile,"rb")),output) | |
output.write(open("MergedFile.pdf","wb")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment