Home > Net >  Python: Merging two PDF/A or converting regular PDF into PDF/A
Python: Merging two PDF/A or converting regular PDF into PDF/A

Time:11-27

I wish to merge two PDF 1/A documents resulting in one single PDF 1/A, is this possible using Python? I have been unable to find a library that supports this functionality. PikePDF can merge them so that the result conforms to PDF 1/A but it isn't a valid PDF 1/A document (it ends up as 1.3).

Another option would be to convert the PikePDF merge result into PDF 1/A but I am unable to find a Python library able to perform the conversion.

Any ideas?

CodePudding user response:

you can use pypdf2 and do something like this to merge pdf

import os, logging
from PyPDF2 import PdfFileMerger

def merge_pdf(self):
    try:
        merger = PdfFileMerger()
        for files in PDF_FILES:
            merger.append(os.path.join(PDF_BASE_PATH,files))
        merger.write(os.path.join(PDF_BASE_PATH,'merged.pdf'))
        merger.close()
    except Exception as e:
        response = str(e)
        logging.info(f'Merge Files : {response}')
        raise e

CodePudding user response:

Did you try to use PyPdf2 module ?

Try following code:

from PyPDF2 import PdfFileMerger

pdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(pdf)

merger.write("result.pdf")
merger.close()
  • Related