1

I have a script to loop through folders and merge the PDF's inside into one. I have a question that is above my pay grade and I don't know how to do, I am very new to programming and am betting on the benevolence of the internet for help.

Is there anyway I can specify the name of the PDF to be saved to be the same as the folder where they are found or even the name of one of the original files?

import PyPDF2
import os
Path = 'C:/Users/Folder location/'

folders = os.listdir(Path)
pdfMerger = PyPDF2.PdfFileMerger()
name = os.listdir(Path)
def pdf_merge(filelist):
    pdfMerger = PyPDF2.PdfFileMerger()
    for file in os.listdir(filelist):
            if file.endswith('.pdf'):
                pdfMerger.append(filelist+'/'+file)
    pdfOutput = open(Path+folder+'/.pdf', 'wb')
    pdfMerger.write(pdfOutput)
    pdfOutput.close()
for folder in folders:
    pdf_merge(Path+folder)
  • 2
    Found this code here: https://stackoverflow.com/questions/33372054/get-folder-name-of-the-file-in-python. `>>> import os >>> path=os.path.dirname("C:/folder1/folder2/filename.xml") >>> path 'C:/folder1/folder2' >>> os.path.basename(path) 'folder2'` Might work for you. – Jhanzaib Humayun Mar 21 '22 at 22:42

0 Answers0