Python-根据列表拆分PDF

时间:2018-09-01 12:52:59

标签: python python-3.x pdf

我正在尝试将PDF分为多个PDF文件,并根据列表将其分成新文件。代码如下:

import sys
import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def splitByStudent(file, group):
    inputPdf = PdfFileReader(open(file,"rb"))
    output = PdfFileWriter()
    path = os.path.dirname(os.path.abspath(file))
    os.chdir(path)
    numpages = int(inputPdf.numPages/len(group))
    for s in group:
        startpage = group.index(s) * numpages
        endpage = startpage + numpages
        newfile = s + ".pdf"
        for i in range(startpage, endpage):
            output.addPage(inputPdf.getPage(i))
            with open(newfile, "wb") as outputStream:
                output.write(outputStream)





BIASL1 = ["Student One", "Student Two"]

file = "filename.pdf"

splitByStudent(file, BIASL1)

PDF“文件名”具有16页,并且生成的第一个文件的名称为“ Student One.pdf”,该文件应具有正确的8页。但是,“ Student Two.pdf”包含原始的全部16页。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

output = PdfFileWriter()行应该在for循环内:

def splitByStudent(file, group):
    inputPdf = PdfFileReader(open(file,"rb"))
    path = os.path.dirname(os.path.abspath(file))
    os.chdir(path)
    numpages = int(inputPdf.numPages/len(group))
    for s in group:
        output = PdfFileWriter()
        startpage = group.index(s) * numpages
        endpage = startpage + numpages
        newfile = s + ".pdf"
        for i in range(startpage, endpage):
            output.addPage(inputPdf.getPage(i))
            with open(newfile, "wb") as outputStream:
                output.write(outputStream)
相关问题