Python Forum

Full Version: Concatenate multiple PDFs using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wrote a code to merge pdf files together
Following is my code:
def merge_pdf(pdf_path,output_pdf):
    ## Create Logger
    logging.basicConfig(filename='LogFile.log',format='%(asctime)s %(message)s',filemode='a')

    logger=logging.getLogger()
    logger.setLevel(logging.DEBUG)
    try:
        os.chdir(pdf_path)
    except:
        logger.error(f"{pdf_path} does not exist")
        return f"{pdf_path} does not exist"

    ###  Get PDF list from the folder
    pdfmerge=[]
    for pdf_file in os.listdir('.'):
        if pdf_file.endswith('.pdf'):
            pdfmerge.append(pdf_file)

    pdfWriter = PyPDF2.PdfFileWriter()

    ###  loop through all PDFs
    for pdf_file in pdfmerge:
        pdfFileObj=open(pdf_file,'rb')
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    ## Open pages for pdf and add them
        for pageNum in range(pdfReader.numPages):
            pageObj=pdfReader.getPage(pageNum)
            pdfWriter.addPage(pageObj)
    try:
        ###  Save PDF
        pdfOutput=open(output_pdf+'.pdf','wb')

        pdfWriter.write(pdfOutput)
        pdfOutput.close()
        logger.debug(f"Successfully concatenated PDFs")
        return "Success"
    except:
        logger.error(f"File Not Created \n Check '{output_pdf}'")
        return f"File Not Created \n Check '{output_pdf}'"

## Enter input folder containing PDFs and Output path to Save COncatenated PDF
merge_pdf(pdf_path=r'input path\PDFs',output_pdf=r'output path\PDFs\Output')
The code is a user input code and is working file but if i put an output pdf which already exists it shows the following error:
Error:
Traceback (most recent call last): File "path\Final.py", line 125, in <module> merge_pdf(pdf_path=r'path',output_pdf=r'C:\Users\MUNISH\Desktop\UI Codes\pathPDFs\Output') File "path\Final.py", line 107, in merge_pdf pdfReader = PyPDF2.PdfFileReader(pdfFileObj) File "C:\Python37\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__ self.read(stream) File "C:\Python37\lib\site-packages\PyPDF2\pdf.py", line 1689, in read stream.seek(-1, 2) OSError: [Errno 22] Invalid argument
I need to change the code so that if i put an output path which already exists, it does not show this error instead goes in the except condition. please help