PDFMiner does not detect all pages

I am trying to extract text from pdfs, but I am running into an error because my script sometimes detects every page of a pdf, and sometimes only detects the first page of a pdf. I even included this line from a previous post on stackoverflow.


Anytime my script extracted just the first page, the script only detected 1 page.

I've even tried another library (PyPDF2) to extract text, but had even worse results.

If I look up the properties of the pdfs that my script mishandles, Adobe clearly shows in the pdf's properties the correct number of pages.

Below is the code I am using. Any recommendations on how I might change my script to detect all pages of a pdf would be appreciated.

import os
from os.path import isfile, join
from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer

pdf_dir = "/dir/pdfs/"
txt_dir = "/dir/txt/"

corpus = (f for f in os.listdir(pdf_dir) if not f.startswith('.') and isfile(join(pdf_dir, f)))
for filename in corpus:
    output_string = StringIO()
    with open(join(pdf_dir, filename), 'rb') as in_file:
        parser = PDFParser(in_file)
        doc = PDFDocument(parser)
        rsrcmgr = PDFResourceManager()
        device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
        interpreter = PDFPageInterpreter(rsrcmgr, device)
        for page in PDFPage.create_pages(doc):
        txt_name = "{}.txt".format(filename[:-4])
        with open(join(txt_dir, txt_name), mode="w", encoding='utf-8') as o:

Read more here: https://stackoverflow.com/questions/64396326/pdfminer-does-not-detect-all-pages

Content Attribution

This content was originally published by Sastorica at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: