Home > Enterprise >  Python merge pdfs without compression
Python merge pdfs without compression

Time:11-09

I want to merge a number of pdfs into one pdf using python, but the method I use right now seems to compress the output:

from pathlib import Path
import PyPDF2

merged_object = PyPDF2.PdfFileMerger()

for file_item in list_of_pdfs:
    if Path(file_item).suffix != '.pdf':
        file_item = f'{file_item}.pdf'
    merged_object.append(PyPDF2.PdfFileReader(str(file_item)))

merged_object.write(str(directory))

I haven't been able to find a way to merge pdfs compressionless, any solutions?

CodePudding user response:

I use to work with PyMuPDF and never noticed compression. The insert_pdf method is suitable to merge pdfs. You can try the following:

import fitz
from pathlib import Path

merged_object = fitz.open(list_of_pdfs.pop(0))

for file_item in list_of_pdfs:
  if Path(file_item).suffix != '.pdf':
      file_item = f'{file_item}.pdf'
  to_append = fitz.open(str(file_item))
  merged_object.insert_pdf(to_append)

merged_object.save(str(directory))
merged_object.close();
  • Related