Combining PDFs using Python and PyPDF2
A job application I went through asked for a cover letter and resume in a single file.
Garrett Mayock posted 2019-02-26 18:44:17 UTC
Today I wanted to combine two PDFs without combining the Word docs. Specifically, there is a resume/cover letter option on a job to which I’m applying, but they only let you upload one file. Since my resume and cover letter are in different file templates, it would be easier to combine them using Python than Word (after printing as PDFs).
I use Anaconda Navigator. PyPDF is not available in the default channel for Anaconda Navigator, so using Anaconda Prompt to “conda install pypdf2” throws an error.
(base) C:\path>conda install pypdf2 Solving environment: failed PackagesNotFoundError: The following packages are not available from current channels: - pypdf2
So instead we have to declare the channel. In this case, the channel is conda-forge.
(base) C:\path>conda install -c conda-forge pypdf2
A few other packages had to update, and it looks like I have to update conda as well …
(base) C:\Users\gmayo>conda update -n base -c defaults conda
Now let’s use it:
import os from PyPDF2 import PdfFileMerger cover_letter = 'filepath/input/covlet.pdf' resume = 'filepath/input/res.pdf' input_paths = [cover_letter, resume] merger = PdfFileMerger() for path in input_paths: merger.append(open(path,'rb')) with open('D:/path/to_merge/output/joined.pdf', 'wb') as arg: merger.write(arg)
So that writes the file perfectly. But if I wanted it to be dynamic to merge any files in the input folder, I’d just make the input_paths list dynamically using glob:
import glob, os from PyPDF2 import PdfFileMerger input_paths = glob.glob('filepath/input/*.pdf') merger = PdfFileMerger() for path in input_paths: merger.append(open(path,'rb')) with open('filepath/output/joined.pdf', 'wb') as arg: merger.write(arg)
This also writes the file perfectly. So that’s my tutorial for the day!
PS: Check it out on my GitHub here: https://github.com/gmayock/PDFmergercontact me