使用PyPDF2将文件合并到多个输出文件

这是导致问题的代码块。 循环将每次追加新文件,这不是我想要完成的。 例如,outputfile1是input1.pdf,outputfile2是input1.pdf + input2.pdf ...

我试图将文件1x.pdf与文件1a.pdf + 1b.pdf + 1c.pdf合并到输出file1.pdf中,然后循环并为2,3和4执行相同的操作。最终结果应该是4个独立的文件。 我错过了什么? 清除泥浆? 预先感谢您的帮助。

i = 1

while i < 5:
    # files to be merged

    input1 = open(Path1+str(i)+"x.PDF", "rb")
    input2 = open(Path2+str(i)+"a.PDF", "rb")
    input3 = open(Path2+str(i)+"b.PDF", "rb")
    input4 = open(Path2+str(i)+"c.PDF", "rb")

    # output files
    output_file = open("/NewFile"+str(i)+".pdf", "wb")

    # add input1 document to output
    merger.append(fileobj = input1, pages = (0, 3, 2), import_bookmarks = False)

    # insert the pages of input2 into the output beginning after the second page
    merger.append(input2)

    # insert the pages of input3 into the output beginning after the second page
    merger.append(input3)

    # insert the pages of input4 into the output beginning after the second page
    merger.append(input4)

    # Write to an output PDF document
    merger.write(output_file)
    output_file.close()

    i += 1
链接地址: http://www.djcxy.com/p/46949.html

上一篇: Using PyPDF2 to merge files into multiple output files

下一篇: Can iText convert PDF to TIFF image(s)?