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

Python PyPDF2尝试提取第一页时返回空白页,python,python-3.x,pdf,pypdf2,Python,Python 3.x,Pdf,Pypdf2,我在创建PDF时遇到了一个问题 首先,我以PdfFileReader的形式打开源pdf并获得第一页,然后,我将第一页添加到输出文件(PDFFileWriter)的页面列表中 但当我检查输出文件时,它只包含一个空白页,下面是我的代码: with open('pdf/'+articolo['itemfilename'], 'rb') as infile: reader = PdfFileReader(infile) writer = PdfFileWriter() #

我在创建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)
我已经向调试器检查过程序是否连接了每一行代码,所以一切都应该正常


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

看起来像代码的这一部分:

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)

这对我很有用。

特定的PDF
所以当你将相同的代码运行到不同的PDF时,它可以正常工作吗?很抱歉,它似乎无法与我测试的另一个PDF一起工作。我尝试在第一个缩进块中缩进代码的最后两行,现在它工作得很好!!问题解决了!!看起来添加到写入程序的页面对象包含对读卡器中数据的引用,而不是副本,因此当读卡器关闭时,写入程序将失去对源数据的访问?