[Python] How to catch a C error MuPDF returns when running the wrapper PyMuPDF?

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the fitz (PyMuPDF) library for a scrawling task that takes content from PDFs and put it into .json files. This is the main function that ultilizes PyMuPDf text-extracting capabilities and have the content saved into a .json file.

    def mu_get_content_from(folder_name, file_name):
    src_file_path = os.path.join(os.getcwd(), "To Do PDF", folder_name, f"{file_name}.pdf")
    dst_file_path = os.path.join(os.getcwd(), "pdfs-2-texts", folder_name, f"{file_name}.json")

    # if os.path.exists(dst_file_path):
    # return True
    os.makedirs(os.path.dirname(dst_file_path), exist_ok=True)

    doc = fitz.open(src_file_path)
    total_pages = 0
    content = ""

    for page in doc:
    page_content = page.get_text()
    if not page_content:
    continue

    page_content = page_content.strip()
    total_pages += 1

    if page_content.endswith('.'):
    content += "\n\n" + page_content
    else:
    content += " " + page_content

    if content.strip() == "":
    error_files.append((folder_name, file_name))

    save_dict = {
    "Title": file_name,
    "Pages": total_pages,
    "Content": content
    }

    with open(dst_file_path, 'w', encoding='utf-8') as fo:
    json.dump(save_dict, fo, ensure_ascii=False, indent=4)

    return True


    While this is the main pipeline in which I'd get go through all PDFs in the "To Do PDF" folder. The get_file_name() function can be though of as a way for me to get the name of the original pdf files and use that to make path names for new .json files inside of the "pdfs-2-texts" folder. error_files is a list of files with no texts (either they are empty or they contain only images).

    for folder in folders:
    folder_path = os.path.join(os.getcwd(), "To Do PDF", folder)
    files = get_file_names(folder_path)
    for file in tqdm(files):
    print(file)
    file_name, ext = os.path.splitext(file)
    if ext.lower() != ".pdf":
    error_files.append((folder, file_name))
    continue
    mu_get_content_from(folder, file_name)


    After running the program for a while, it would print out this error without actually stopping the program. So, the program would output this:

    MuPDF error: syntax error: unknown keyword: '41r020I'

    MuPDF error: syntax error: unknown keyword: '41r020I'

    MuPDF error: syntax error: unknown keyword: '41r020I'

    MuPDF error: syntax error: unknown keyword: '41r020I'

    MuPDF error: syntax error: syntax error in content stream


    But would still continue to take in contents of the files. As a result, there are certain .json files that would have gibberish as its text content and some .json files would collect all of its intended content, even though both cases flagged for an error. Is there a way to catch the error and give me a holistic of all the files with potential errors in them?

    I did some research and found that PyMuPDf is a Python wrapper of MuPDF. What this means is that we're running MuPDF, which is a C library in Python. So, the Python code would print out the error of the C functions in the C library, instead of the usual error flag in Python. I understand their motivation of why they chose this implementation. But I want to find a way to catch that flag by C for my case.

    Continue reading...

Compartilhe esta Página