Last active
August 16, 2024 19:53
Python oneline safe pdf merging showcasing using a way of lambda handling exceptions
This file contains hidden or 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
# git clone --branch v0.4 --depth 1 --single-branch https://github.com/pmaupin/pdfrw | |
# PYTHONPATH="$PWD/pdfrw:$PYTHONPATH" python3 safepdfmerge.py /path/to/target/dir /path/to/my/file1.pdf /path/to/my/file2.pdf /paths/to/other/files.pdf | |
import sys, os, pdfrw; safe_read_pdf_pages = lambda path: exec(f"try: res = pdfrw.PdfReader({path!r}).pages;\nexcept: print({path!r}, sys.exc_info()); res = []", globals(), globals()) or res; writer = pdfrw.PdfWriter(); [writer.addpages(safe_read_pdf_pages(inpfn)) for inpfn in sys.argv[2:]]; os.makedirs(sys.argv[1], exist_ok = True); writer.write(os.path.join(sys.argv[1], os.path.basename(sys.argv[2]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment