Skip to content

Instantly share code, notes, and snippets.

@BjornFJohansson
Created January 19, 2024 17:25
Show Gist options
  • Save BjornFJohansson/dfe0acfef0fd3b4113d5a985ac6d540c to your computer and use it in GitHub Desktop.
Save BjornFJohansson/dfe0acfef0fd3b4113d5a985ac6d540c to your computer and use it in GitHub Desktop.
#!/home/bjorn/miniforge3/envs/bjorn311/bin python3
# -*- coding: utf-8 -*-
# https://pdfstandalone.com/en/merge-pdf
import sys
from pathlib import Path
from borb.pdf.document.document import Document
from borb.pdf.pdf import PDF
from tqdm import tqdm
script, *cliarg = sys.argv
pdfpaths = [Path(p) for p in cliarg] or sorted(Path(".").glob("*.pdf"))
output_document = Document()
outpath = Path("output.pdf")
try:
pdfpaths.remove(outpath)
except ValueError:
pass
for pdfpath in tqdm(pdfpaths):
with open(pdfpath, "rb") as pdf_file_handle:
pdf = PDF.loads(pdf_file_handle)
output_document.add_document(pdf)
with open(outpath, "wb") as pdf_out_handle:
PDF.dumps(pdf_out_handle, output_document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment