commit 7d3b01c10e7c6dcf78b3190f201b2dd5f2ee9f05 Author: cgarbin Date: Mon Jul 9 07:37:21 2018 -0400 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91c9767 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 cgarbin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f56bb3d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# python-combine-pdfs diff --git a/python-combinepdf.py b/python-combinepdf.py new file mode 100644 index 0000000..a10ab13 --- /dev/null +++ b/python-combinepdf.py @@ -0,0 +1,47 @@ +""" +Combines PDF files into one PDF file. + +Sources for this code: + +* https://automatetheboringstuff.com/chapter13/ +* https://www.geeksforgeeks.org/working-with-pdf-files-in-python/ +* https://pythonhosted.org/PyPDF2/ +* https://stackoverflow.com/a/49927541/336802 + +This program combines all specified PDF files into one PDF file. The +files are combined in the order they are given. Use '-o output.pdf' to +specify the output file name (defaults to combined.pdf if not specified). +""" + +from argparse import ArgumentParser +import contextlib +import PyPDF2 + + +def main(): + ap = ArgumentParser( + description='Combines several PDF files into one file.') + + # The files to combine + ap.add_argument('files', metavar='file1 file2 ...', + help='Files to combine', nargs='+') + + # The output file (defaults to combined.pdf if not specified) + ap.add_argument('-o', '--output', nargs='?', + const='combined.pdf', default='combined.pdf', + help='Output file name (combined.pfd, if not specified)') + + args = ap.parse_args() + + # Workaround for PyPDF2 empty output file: keep input files open + # See https://stackoverflow.com/a/49927541/336802 + with contextlib.ExitStack() as stack: + pdfMerger = PyPDF2.PdfFileMerger() + files = [stack.enter_context(open(pdf, 'rb')) for pdf in args.files] + for f in files: + pdfMerger.append(f) + with open(args.output, 'wb') as f: + pdfMerger.write(f) + +if __name__ == '__main__': + main()