Файл еще не извлечен, поэтому вы не можете работать с ним с помощью open()
.
Это нормально, потому что PdfFileReader хочет поток; поэтому мы можем предоставить его, используя BytesIO . В приведенном ниже примере распакованные байты передаются в BytesIO, который превращает их в поток для PdfFileReader. Если вы не указали BytesIO, вы получите: AttributeError: 'bytes' object has no attribute 'seek'
.
import PyPDF2,zipfile
from io import BytesIO
with zipfile.ZipFile('sample.zip','r') as z:
filename = z.namelist()[0]
pdf_file = PyPDF2.PdfFileReader(BytesIO(z.read(filename)))
Результат:
In [20]: pdf_file
Out[20]: <PyPDF2.pdf.PdfFileReader at 0x7f01b61db2b0>
In [21]: pdf_file.getPage(0)
Out[21]:
{'/Type': '/Page',
'/Parent': {'/Type': '/Pages',
'/Count': 2,
'/Kids': [IndirectObject(4, 0), IndirectObject(6, 0)]},
'/Resources': {'/Font': {'/F1': {'/Type': '/Font',
'/Subtype': '/Type1',
'/Name': '/F1',
'/BaseFont': '/Helvetica',
'/Encoding': '/WinAnsiEncoding'}},
'/ProcSet': ['/PDF', '/Text']},
'/MediaBox': [0, 0, 612, 792],
'/Contents': {}}