First commit

This commit is contained in:
cgarbin 2018-07-09 07:37:21 -04:00
commit 7d3b01c10e
4 changed files with 70 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode

21
LICENSE Normal file
View File

@ -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.

1
README.md Normal file
View File

@ -0,0 +1 @@
# python-combine-pdfs

47
python-combinepdf.py Normal file
View File

@ -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()