1

I am using PyPDF2 and tika to extract text from .pdf and .htm files. I am running into the following error: "PyPDF2.utils.PdfReadError: EOF marker not found"

I have seen multiple posts on the issue yet none included a solution.

Here's the code I'm using:

from xlwt import Workbook

import PyPDF2, os

from tika import parser


wb = Workbook()

sheet1 = wb.add_sheet('Sheet 1')
sheet1.write(0, 0, 'file name')
sheet1.write(0, 1, 'file content')

pdfFiles = []
folderPath = 'C:/Users/Turing/Desktop/workingFiles' #! define the path for the folder including input files

for filename in os.listdir(folderPath):
    if filename.endswith('.htm') or filename.endswith('.pdf'):
        pdfFiles.append(filename)

pdfFiles.sort(key=str.lower)

row = 0

for filename in pdfFiles:
    row = row + 1
    #print(filename)
    sheet1.write(row, 0, filename)  # write the name of the file to column number 0 of output
    filename = folderPath+'\\'+filename
    pdfFileObj = open(filename, 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    raw = parser.from_file(filename)
    #print(raw['content'])
    sheet1.write(row, 1, raw['content']) # write the content of the input doc to column number 1 of the output

wb.save('MRS.xls')

I have uploaded one of the problematic files for your reference.

segilmez
  • 35
  • 5

1 Answers1

0

You are reading a HTML file with PyPDF2.PdfFileReader, which is expecting a PDF file. Probably easiest to split into

pdfFiles = []
htmFiles = []
for filename in os.listdir(folderPath):
    if filename.endswith('.pdf'):
        pdfFiles.append(filename)
    if filename.endswith('.htm'):
        htmFiles.append(filename)

and parse those separately.

Joozd
  • 501
  • 2
  • 14