How to append PDF pages using PyPDF2

后端 未结 5 864
灰色年华
灰色年华 2020-12-16 19:03

Is anybody has experience merging two page of PDF file into one using python lib PyPDF2. When I try page1.mergePage(page2) it results with page2 overlayed page1

5条回答
  •  醉酒成梦
    2020-12-16 19:45

    As I'm searching the web for python pdf merging solution, I noticed that there's a general misconception with merging versus appending.

    Most people call the appending action a merge but it's not. What you're describing in your question is really the intended use of mergePage which should be called applyPageOnTopOfAnother but that's a little long. What you are (were) looking for is really appending two files/pages into a new file.

    Appending PDF files

    Using the PdfFileMerger class and its append method.

    Identical to the merge() method, but assumes you want to concatenate all pages onto the end of the file instead of specifying a position.

    Here's one way to do it taken from pypdf Merging multiple pdf files into one pdf:

    from PyPDF2 import PdfFileMerger, PdfFileReader
    
    # ...
    
    merger = PdfFileMerger()
    
    merger.append(PdfFileReader(file(filename1, 'rb')))
    merger.append(PdfFileReader(file(filename2, 'rb')))
    
    merger.write("document-output.pdf")
    

    Appending specific PDF pages

    And to append specific pages of different PDF files, use the PdfFileWriter class with the addPage method.

    Adds a page to this PDF file. The page is usually acquired from a PdfFileReader instance.

    file1 = PdfFileReader(file(filename1, "rb"))
    file2 = PdfFileReader(file(filename2, "rb"))
    
    output = PdfFileWriter()
    
    output.addPage(file1.getPage(specificPageIndex))
    output.addPage(file2.getPage(specificPageIndex))
    
    outputStream = file("document-output.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    

    Merging two pages into one page

    Using mergePage

    Merges the content streams of two pages into one. Resource references (i.e. fonts) are maintained from both pages. The mediabox/cropbox/etc of this page are not altered. The parameter page’s content stream will be added to the end of this page’s content stream, meaning that it will be drawn after, or “on top” of this page.

    file1 = PdfFileReader(file(filename1, "rb"))
    file2 = PdfFileReader(file(filename2, "rb"))
    
    output = PdfFileWriter()
    
    page = file1.getPage(specificPageIndex)
    page.mergePage(file2.getPage(specificPageIndex))
    
    output.addPage(page)
    
    outputStream = file("document-output.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    

提交回复
热议问题