binary-husky/gpt_academic · error · RuntimeError

抱歉, 我们暂时无法解析此PDF文档: {fp}。

Error message

抱歉, 我们暂时无法解析此PDF文档: {fp}。

What it means

read_and_clean_pdf_text's second stage builds a font-size histogram from PyMuPDF spans and calls max() on it. If the PDF yields no text spans, max() raises ValueError and the bare except converts it to this generic RuntimeError. The most common cause is an image-only/scanned PDF, but any exception while computing the main font is hidden.

Source

Thrown at crazy_functions/crazy_utils.py:444

            meta_txt.extend([" ".join(["".join([wtf['text'] for wtf in l['spans']]) for l in t['lines']]).replace(
                '- ', '') for t in text_areas['blocks'] if 'lines' in t])
            meta_font.extend([np.mean([np.mean([wtf['size'] for wtf in l['spans']])
                             for l in t['lines']]) for t in text_areas['blocks'] if 'lines' in t])
            if index == 0:
                page_one_meta = [" ".join(["".join([wtf['text'] for wtf in l['spans']]) for l in t['lines']]).replace(
                    '- ', '') for t in text_areas['blocks'] if 'lines' in t]

        ############################## <第 2 步,获取正文主字体> ##################################
        try:
            fsize_statistics = {}
            for span in meta_span:
                if span[1] not in fsize_statistics: fsize_statistics[span[1]] = 0
                fsize_statistics[span[1]] += span[2]
            main_fsize = max(fsize_statistics, key=fsize_statistics.get)
            if REMOVE_FOOT_NOTE:
                give_up_fize_threshold = main_fsize * REMOVE_FOOT_FFSIZE_PERCENT
        except:
            raise RuntimeError(f'抱歉, 我们暂时无法解析此PDF文档: {fp}。')
        ############################## <第 3 步,切分和重新整合> ##################################
        mega_sec = []
        sec = []
        for index, line in enumerate(meta_line):
            if index == 0:
                sec.append(line[fc])
                continue
            if REMOVE_FOOT_NOTE:
                if meta_line[index][fs] <= give_up_fize_threshold:
                    continue
            if ffsize_same(meta_line[index][fs], meta_line[index-1][fs]):
                # 尝试识别段落
                if meta_line[index][fc].endswith('.') and\
                    (meta_line[index-1][fc] != 'NEW_BLOCK') and \
                    (meta_line[index][fb][2] - meta_line[index][fb][0]) < (meta_line[index-1][fb][2] - meta_line[index-1][fb][0]) * 0.7:
                    sec[-1] += line[fc]
                    sec[-1] += "\n\n"
                else:

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Confirm the PDF contains selectable text rather than page images.
  2. OCR the PDF first, for example with OCRmyPDF or Tesseract, and retry.
  3. Use an alternative parser such as Grobid or doc2x if available in this project.
  4. Remove password protection/repair the PDF before uploading.
  5. Replace the bare except with a specific check for empty meta_span and include the original exception.

Example fix

# before
try:
    fsize_statistics = {}
    ...
    main_fsize = max(fsize_statistics, key=fsize_statistics.get)
except:
    raise RuntimeError(f'抱歉, 我们暂时无法解析此PDF文档: {fp}。')

# after
if not meta_span:
    raise RuntimeError(f'PDF has no extractable text; run OCR first: {fp}')
try:
    ...
    main_fsize = max(fsize_statistics, key=fsize_statistics.get)
except Exception as e:
    raise RuntimeError(f'抱歉, 我们暂时无法解析此PDF文档: {fp}: {e}') from e
Defensive patterns

Strategy: validation

Validate before calling

import fitz

def pdf_has_extractable_text(fp, sample_pages=3) -> bool:
    with fitz.open(fp) as doc:
        pages = doc[:sample_pages] if len(doc) >= sample_pages else doc
        return any(page.get_text().strip() for page in pages)

Type guard

def is_text_pdf(fp: str) -> bool:
    try:
        with fitz.open(fp) as doc:
            return any(page.get_text().strip() for page in doc)
    except Exception:
        return False

Try / catch

try:
    file_content, page_one = read_and_clean_pdf_text(fp)
except RuntimeError as e:
    if "无法解析此PDF" in str(e):
        route_pdf_to_ocr_or_alternate_parser(fp)
    else:
        raise

Prevention

When it happens

Trigger: fitz.open() succeeds but page.get_text('dict') produces no lines/spans, so meta_span is empty; or span data is malformed. Scanned pages, image-only exports, some encrypted PDFs, and unusual PDF generators trigger it.

Common situations: Scanned papers loaded into PDF summarization/translation; a PDF containing only images; a password-protected or damaged PDF; a version-specific PyMuPDF extraction issue.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/f568ba26763375c3. Report an issue: GitHub.