Python Forum
Concatenate multiple PDFs using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatenate multiple PDFs using python
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,431 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Comparing PDFs CaseCRS 5 1,143 Apr-01-2023, 05:46 AM
Last Post: DPaul
  Concatenate str JohnnyCoffee 2 2,879 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  download pubmed PDFs using pubmed2pdf in python Wooki 8 5,366 Oct-19-2020, 03:06 PM
Last Post: jefsummers
  Concatenate two dataframes moralear27 2 1,833 Sep-15-2020, 08:04 AM
Last Post: moralear27
  How to compare two PDFs for differences Normanie 2 2,352 Jul-30-2020, 07:31 AM
Last Post: millpond
  can only concatenate str (not "int") to str gr3yali3n 6 4,018 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  How to concatenate multiple dataframes rajeshE 1 1,994 Mar-02-2020, 06:37 AM
Last Post: scidam
  Concatenate two dictionaries harish 3 2,321 Oct-12-2019, 04:52 PM
Last Post: strngr12
  Most optimized way to merge figures from multiple PDFs into one PDF page? dmm809 1 2,012 May-22-2019, 10:32 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020