尝试提取第一页时,PyPDF2返回空白页

时间:2019-07-15 09:12:20

标签: python python-3.x pdf pypdf2

我在创建PDF时遇到问题。

首先,我将PdfFileReader的源pdf打开并获取第一页,然后,将第一页添加到输出文件(PDFFileWriter)的页面列表中。

但是当我去检查输出文件时,它只包含一个空白页,这是我的代码:

with open('pdf/'+articolo['itemfilename'], 'rb') as infile:
        reader = PdfFileReader(infile)
        writer = PdfFileWriter() #writer.addPage(reader.getPage(i))
        if articolo['copertina'] == 1:    #this is just a check it works I verified
            writer.addPage(reader.getPage(0))
with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
    writer.write(outfile)

我已经与调试器一起检查了程序是否连接了代码的每一行,所以一切都应该没事。

如果您有任何疑问,请告诉我。

1 个答案:

答案 0 :(得分:1)

看起来像这段代码:

with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
    writer.write(outfile)

应在第一个缩进块内缩进,如下所示:

with open('pdf/'+articolo['itemfilename'], 'rb') as infile:
    reader = PdfFileReader(infile)
    writer = PdfFileWriter() #writer.addPage(reader.getPage(i))
    if articolo['copertina'] == 1:    #this is just a check it works I verified
        writer.addPage(reader.getPage(0))
    with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
        writer.write(outfile)

这对我有用。