PyPDF2 complete clone of file

本秂侑毒 提交于 2019-12-13 18:57:08

问题


I am trying to copy a PDF in its entirety using PyPDF2, the following code copies the content but not the outline of the pdf.

here is a sample pdf and use the code as follows python test.py <input pdf> <output dest>

Here is the code that I have so far.

from PyPDF2 import PdfFileWriter, PdfFileReader
import sys
import os.path

def main(argv):
    if not os.path.isfile(argv[0]) and \
    not os.path.isfile(argv[1]):
        print("Invalid path")
        sys.exit()
    input_pdf = PdfFileReader(open(argv[0], "rb"))
    output_pdf = PdfFileWriter()
    input_pdf_pages = input_pdf.getNumPages()
    for i in range(0, input_pdf_pages):
        output_pdf.addPage(input_pdf.getPage(i))
    output_pdf.write(open(argv[1], "wb"))

if __name__ == "__main__":
    main(sys.argv[1:])

回答1:


PdfFileWriter does have a number of methods for copying an entire file: appendPagesFromReader, cloneReaderDocumentRoot, and cloneDocumentFromReader.

However, I can't get them to work properly either. ;-) You might have better luck.



来源:https://stackoverflow.com/questions/48599243/pypdf2-complete-clone-of-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!