Pdf converter

 import os

from PyPDF2 import PdfFileMerger, PdfFileReader


def merge_pdfs(path_dir):

    pdfs = [file for file in os.listdir(path_dir) if file.endswith('.pdf')]

    merger = PdfFileMerger()

    for pdf in pdfs:

        with open(os.path.join(path_dir, pdf), 'rb') as file:

            pdf_reader = PdfFileReader(file)

            merger.append(pdf_reader)

    with open(os.path.join(path_dir, 'merged_file.pdf'), 'wb') as output_file:

        merger.write(output_file)

    print('PDF files merged successfully.')


if __name__ == '__main__':

    path_dir = '/path/to/directory/with/pdfs'

    merge_pdfs(path_dir)


Comments

Popular posts from this blog

Best crypto currency

## **India vs Pakistan: A Century-Old Rivalry and the Struggle for Peace**