How to merge and combine PDF files using Python language?

In this tutorial I will show you how to merge and combine two or more PDF files into one PDF file using Python 3.

VeryUtils PythonPDF Library is a Python Library for splitting and merging the pages of PDF and PDF/A conform documents with practical additional functions. It can process multiple input and output documents simultaneously. PDF Merge Split is available as shell tool for batch processing with the command line and as API to be integrated with Python, PHP, C#, Visual Basic, Java or C/C++.

PythonPDF Library can be purchased on this web page,

https://veryutils.com/pythonpdf-library-source-code

after you purchase it, you will get a download URL to the latest version of PythonPDF Library, please download and unzip it to a folder, such as,

D:\downloads\python-pdfrw

before you use it, please set the main path to "PYTHONPATH" first, for example,

set PYTHONPATH=D:\downloads\python-pdfrw

then please go to "examples" folder, run following command line to test the PDF Merging function,

python cat.py testcmd.pdf watermark.pdf

You will get a merged PDF file after a few seconds, in the following screenshot, the pages #1-#32 are come from first PDF file, the pages #33-#43 are come from second PDF file,

image

The following is the Python source code of cat.py file,

--------------------------------------------------
import sys
import os

from pdfrw import PdfReader, PdfWriter, IndirectPdfDict

inputs = sys.argv[1:]
assert inputs
outfn = 'cat.' + os.path.basename(inputs[0])

writer = PdfWriter()
for inpfn in inputs:
    writer.addpages(PdfReader(inpfn).pages)

writer.trailer.Info = IndirectPdfDict(
    Title='your title goes here',
    Author='your name goes here',
    Subject='what is it all about?',
    Creator='some script goes here',
)
writer.write(outfn)
--------------------------------------------------

This cat.py file demonstrates two features:

1) Concatenating multiple input PDFs.
2) Adding metadata to the PDF.

If you have any question for the PythonPDF Library, please feel free to let us know, we are glad to assist you asap,

https://veryutils.com/contact

No votes yet.
Please wait...

Related Posts

Leave a Reply

Your email address will not be published.