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

标签 python python-3.x pdf pypdf

我在创建 PDF 时遇到问题。

首先,我将源 pdf 作为 PdfFileReader 打开并获取第一页,之后,我将第一页添加到输出文件 (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)

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

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)

这对我有用。

关于python - PyPDF2 尝试提取第一页时返回空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036567/

相关文章:

.net - 如何使用 PDFsharp .NET 库将 PDF 页面导出为图像?

python - 将 PDF 文件转换为多页图像

python - 如何遵循 HINT : Use a callable instead, 例如,使用 `dict` 而不是 `{}` ?

python - 如何在Python中检查字符串的 "natural"数据类型?

python - 尝试执行字符串插值时获取 "invalid syntax"

Python Reportlab 段落不跨越下一页

python - 向旧 csv 文件添加新行

python - 计算来自 linux shell 的 xml 元素的数量

python - 如何创建关联矩阵

python - Django 不匹配 url 中的 unicode