Bottom Page

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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  can only concatenate str (not "int") to str gr3yali3n 6 514 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  How to compare two PDFs for differences Normanie 0 178 Mar-25-2020, 04:24 PM
Last Post: Normanie
  How to concatenate multiple dataframes rajeshE 1 254 Mar-02-2020, 06:37 AM
Last Post: scidam
  Concatenate two dictionaries harish 3 378 Oct-12-2019, 04:52 PM
Last Post: strngr12
  Most optimized way to merge figures from multiple PDFs into one PDF page? dmm809 1 465 May-22-2019, 10:32 PM
Last Post: micseydel
  Merging pdfs with PyPDF2 Pedroski55 0 1,152 Mar-07-2019, 11:58 PM
Last Post: Pedroski55
Photo How to Extract Specific Words from PDFs with Python danvsv 1 1,777 Jan-17-2019, 11:07 AM
Last Post: Larz60+
  Python split and concatenate saravanatn 5 1,287 Jul-31-2018, 08:29 AM
Last Post: Axel_Erfurt
  reading pdfs in windows10 - Python 3.6 cobra 1 2,085 May-10-2018, 09:40 PM
Last Post: nilamo
  How to parse pdfs in Python CharType 2 1,732 Jan-09-2017, 11:56 PM
Last Post: Blue Dog

Forum Jump:


Users browsing this thread: 1 Guest(s)