make a script that takes all pdf files in a folder and merges them to one file in python

To merge multiple PDF files into one using Python, you can use the PyPDF2 library. Here's an example script:

main.py
import os
from PyPDF2 import PdfFileMerger

# path to the folder containing the PDF files to be merged
folder_path = '/path/to/pdf/folder'

# create a list of PDF files in the folder
pdf_files = [f for f in os.listdir(folder_path) if f.endswith('.pdf')]

# create a PdfFileMerger object
merger = PdfFileMerger()

# merge the PDF files
for filename in pdf_files:
    file_path = os.path.join(folder_path, filename)
    merger.append(file_path)

# write the merged PDF to a file
output_path = '/path/to/merged/file.pdf'
merger.write(output_path)

# close the PdfFileMerger object
merger.close()
593 chars
24 lines

In this script, we first create a list of all the PDF files in the specified folder. Then we create a PdfFileMerger object and append each PDF file to it. Finally, we write the merged PDF to a file and close the PdfFileMerger object.

gistlibby LogSnag