Document Extraction with GPT-4o: Parse PDF, Word and Excel Files

Andreas Nigg
Andreas Nigg · Co-Founder, Pondhouse Data
Published Updated 9 min read
Document Extraction with GPT-4o: Parse PDF, Word and Excel Files
On this page

GPT-4o can parse PDF, Word and Excel documents reliably if you treat them as pictures: render every page as an image, send the images to GPT-4o and ask it to return the text. This post shows the Python code for all three formats. Word and Excel files are converted to PDF with headless LibreOffice first. At the prices when we wrote this, extraction cost about 0.32 to 0.62 cents per page.

Why is this needed at all? LLM applications, RAG pipelines in particular, need the content of "human readable" documents like PDFs, Word documents and Excel sheets, and LLMs can't read these file formats directly.

People have worked on this problem for decades. The most common approaches were rule-based extraction and machine learning based solutions, and both have limits. Rule-based solutions are very fast, but they only handle a narrow variety of documents. Machine learning based solutions are more flexible, but very slow, and they still struggle with the sheer variety of document formats and layouts.

In our article about using LLMs to extract data, we discussed how and why LLMs might finally solve this problem. Using a billion-parameter model to extract text looks ridiculous, but the results were miles better than anything we had before. If you want more background on how LLMs help with document parsing, we recommend reading that article first.

This article builds on it and shows how to extract data with GPT-4o, which is arguably one of the best LLMs with vision input available at the time of writing. We also show how to extract data from Word and Excel documents.

Update (November 2026): OpenAI has deprecated GPT-4o, and the gpt-4o-2024-08-06 snapshot used below shuts down on January 14, 2027 (see the deprecations page). The approach works the same with newer vision models, but they tokenize images differently: according to OpenAI's vision guide, the 85-token low detail cost and the 512px tiles described below apply to GPT-4o, and on some newer models low does not always use fewer tokens than high. Recalculate the per-page cost for the model you use.

The strategy: parsing documents as images with a vision LLM

Our introductory article on document extraction with LLMs explains the strategy in detail. In short:

  1. Convert the document into an image.
  2. Send the image to an LLM with vision capabilities.
  3. Prompt the LLM to extract the text from the image.

Taking screenshots of documents, sending them to an LLM and "asking" it to kindly return the text sounds a little ridiculous at first. On second thought it makes sense. We ask the LLM to "read" the document by giving it a visual representation, which is pretty much how we mere humans extract text from documents too.

This strategy has worked very well for us in practice, for three reasons.

LLMs are very good at understanding document context, and they identify layouts, tables and other document structures.

Prompting lets us define the output. For example, we can ask the model to skip headers, footers and other irrelevant parts, or to format headlines in a specific way (such as Markdown).

Because we screenshot the whole page, the LLM can also extract information from images embedded in the document, even charts. See our article on image analysis with LLMs for more information.

Setting up Python for GPT-4o document parsing

We'll use good old Python for this.

  1. Create and activate a venv or conda environment.

  2. Install the following pip packages

    1pip install openai pillow pypdfium2 backoff

Parsing PDF files with GPT-4o

We'll start with what is, surprisingly, the easiest document format: PDFs.

(Why surprisingly? Historically, PDFs are a pain to work with. They have no standardized layout, no concept of tables (tables in PDFs are just lines and text!) and they can contain images, charts and visualizations of all kinds. Conventional data extraction systems struggled with PDFs for exactly these reasons.)

  1. First, import the packages and initialize the GPT-4o API client.

    1from openai import AsyncOpenAI, RateLimitError
    2from io import BytesIO
    3import asyncio
    4import base64
    5import pypdfium2 as pdfium
    6import backoff
    7
    8client = AsyncOpenAI(api_key="your-api-key")

    Note: We use the async version of the OpenAI SDK so that we can send multiple requests to the API concurrently.

  2. Next, load the PDF file

1 pdf_file = "mypdf.pdf"
2 pdf = pdfium.PdfDocument(pdf_file)
  1. To create the images we send to GPT-4o, loop through all the pages and convert each one to an image with pypdfium2 .

    One important detail: GPT-4o requires the images to be base64 encoded

    1images = []
    2for i in range(len(pdf)):
    3 page = pdf[i]
    4 image = page.render(scale=4).to_pil()
    5 buffered = BytesIO()
    6 image.save(buffered, format="JPEG")
    7 img_byte = buffered.getvalue()
    8 img_base64 = base64.b64encode(img_byte).decode("utf-8")
    9 images.append(img_base64)
  2. Now send the images to GPT-4o and wait for the extracted text. The parse_page_with_gpt function takes a single base64 encoded page and returns the extracted text. We then use asyncio to send all the pages of the document concurrently, so they run more or less in parallel.

    Note that we use the detail = low parameter in the API call. OpenAI offers two different image qualities : low and high. With low, OpenAI's image pipeline downsamples the image to 512x512 pixels. high also downsamples the image to 512x512 pixels, but then additionally creates detailed crops of 512x512 pixels each. The model first sees the low fidelity image to get an overview, followed by the detailed crops. The catch: if the details you want to extract are larger than 512 pixels, the detailed crops are useless. For document extraction you almost always get better results (at lower cost) with the low quality setting.

    1@backoff.on_exception(backoff.expo, RateLimitError)
    2async def parse_page_with_gpt(base64_image: str) -> str:
    3
    4 messages=[
    5 {
    6 "role": "system",
    7 "content": """You are a system to extract knowledge from documents.
    8 We want to add this knowledge to a wiki afterwards.
    9 Please extract the knowledge of this document.
    10 Please only extract the information given in the document.
    11 Do not answer with any additional explanations or text.
    12 I want to take your answer 1 to 1 and put it in the wiki.
    13 Please do not reference graphical elements or visualization in your answer. Just answer with the extracted text.
    14 Make sure that text at the end of the page as well as text at the beginning of the page are also at the end and beginning of your extraction - as this might be continuations of the previous and next page.
    15 """
    16 },
    17 {
    18 "role": "user",
    19 "content": [
    20 {"type": "text", "text": "Extract knowledge from this document"},
    21 {
    22 "type": "image_url",
    23 "image_url": {
    24 "url": f"data:image/jpeg;base64,{base64_image}",
    25 "detail": "low"
    26 },
    27 },
    28 ],
    29 }
    30 ]
    31
    32 response = await client.chat.completions.create(
    33 model="gpt-4o-2024-08-06",
    34 messages=messages,
    35 max_tokens=4096,
    36 )
    37
    38 return response.choices[0].message.content or ""
    39
    40text_of_pages = await asyncio.gather(*[parse_page_with_gpt(image) for image in images])

    We use the backoff library to handle rate limiting. This matters, because the OpenAI API has fairly strict rate limits.

  3. As a final step, join the parsed per-page texts into a single document text.

    1document_text = "\n".join(text_of_pages)

That's it. With a few lines of code, we extracted the text from a PDF file with GPT-4o. In our experience, the parsing quality isn't comparable to conventional methods, in the most positive way imaginable.

How much does extracting a PDF with GPT-4o cost?

LLM document parsing is easy to set up, but what does it cost with OpenAI's models?

As usual, you pay for both the input tokens and the generated output tokens.

The output tokens are easy to calculate: they are the tokens of the text extracted from the document. As of November 2026, the rate for GPT-4o is $10 per 1 million output tokens (and $2.50 per million input tokens).

In our experience, a very full page has around 600 tokens, while most pages have closer to 300. So one page costs about 0.3 to 0.6 cents in output tokens.

Then there are the input tokens, which are surprisingly cheap for low-res images: OpenAI charges 85 input tokens per image, which adds 0.0213 cents per page.

Overall, you pay about 0.32 to 0.62 cents per page.

Why send one page at a time instead of the whole document?

Why go through the hassle of splitting the document into pages and sending them to the API one by one? Because of the resolution limits of the GPT-4o vision model. As outlined above, the image is downsampled to 512x512 pixels, so the text has to stay readable at that resolution. A single A4 page is usually readable enough at 512x512.

Parsing Word (DOCX) documents with GPT-4o

Next up: Word documents in the DOCX format. Why is this different from parsing PDFs? Because Microsoft, in a move that can only be described as absolutely genius (sarcasm...), decided not to store any page break information in the file format. The rendering software decides where pages break. (That's also one reason why different DOCX renderers show the same document slightly differently.) For us, it means we can't simply take screenshots of the pages, because we don't know where a page starts and ends.

We ran a lot of experiments and found that the best way is to transform the Word document to PDF first and then use the PDF strategy described above.

Yes, dear reader, the best way to parse DOCX Word documents seems to be:

  1. Convert the docx to pdf
  2. Convert the document into an image.
  3. Send the image to an LLM with vision capabilities.
  4. Prompt the LLM to extract the text from the image.

(I can't believe I'm writing this, but the output quality is indeed very, very good!)

Steps 2, 3 and 4 are covered in the PDF section, so let's focus on converting a DOCX to PDF. Again there are several ways. The one that worked best for us is to run LibreOffice in headless mode, render the DOCX and save it as PDF.

  1. Install LibreOffice. On Ubuntu, you can simply use apt install libreoffice. For other platforms, see the LibreOffice download page.

  2. Then create a function that returns the LibreOffice executable for your platform.

    1import sys
    2def libreoffice():
    3 if sys.platform == 'darwin':
    4 return '/Applications/LibreOffice.app/Contents/MacOS/soffice'
    5 if sys.platform == 'win32':
    6 return 'C:\\Program Files\\LibreOffice\\program\\soffice.exe'
    7 return 'libreoffice'
  3. Next, create the conversion function. It takes a BytesIO as input and returns a BytesIO object containing the PDF file. If you're not familiar with BytesIO: it's a binary stream backed by an in-memory bytes buffer.

    • First, we create a temporary file from the BytesIO input (LibreOffice only works with files, not streams)
    • Then we run the LibreOffice command to convert the file to PDF
    • After checking that the conversion succeeded, we read the generated PDF file and return it as BytesIO.
    1import os
    2import re
    3import subprocess
    4import tempfile
    5
    6def convert_to_pdf(source: BytesIO, timeout=20) -> BytesIO:
    7 # Create a temp directory to store the input and output file
    8 with tempfile.TemporaryDirectory() as temp_dir:
    9 temp_input_path = os.path.join(temp_dir, "input_file")
    10 with open(temp_input_path, "wb") as temp_file:
    11 temp_file.write(source.getvalue())
    12
    13 # Construct the LibreOffice command
    14 args = [
    15 libreoffice(),
    16 '--headless',
    17 '--convert-to',
    18 'pdf',
    19 '--outdir',
    20 temp_dir,
    21 temp_input_path
    22 ]
    23
    24 process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
    25
    26 # Check for the output filename in the LibreOffice output.
    27 # The output is something like -> <filename> using filter
    28 filename_match = re.search('-> (.*?) using filter', process.stdout.decode())
    29
    30 if filename_match is None:
    31 raise Exception(process.stdout.decode())
    32
    33 output_filename = filename_match.group(1)
    34 output_path = os.path.join(temp_dir, output_filename)
    35
    36 # Read the generated PDF file
    37 with open(output_path, "rb") as pdf_file:
    38 pdf_content = BytesIO(pdf_file.read())
    39
    40 return pdf_content

Now put everything together:

1docx_path = "mydoc.docx"
2with open(docx_path, "rb") as docx_file:
3 docx_content = BytesIO(docx_file.read())
4pdf = convert_to_pdf(docx_content)
5
6# ... continue with the pdf parsing as outlined in the chapter above...

Parsing Excel (XLSX) files with GPT-4o

For completeness: Excel files have the same problem as Word files. It isn't clear where a page starts and ends. Once again, we use LibreOffice to convert the Excel file to PDF and then continue as usual.

The process is exactly the same as for DOCX files, so we can reuse the functions from above.

1xlsx_path = "mydoc.xlsx"
2with open(xlsx_path, "rb") as xlsx_file:
3 xlsx_content = BytesIO(xlsx_file.read())
4pdf = convert_to_pdf(xlsx_content)
5
6# ... continue with the pdf parsing as outlined in the chapter above...

Note: LibreOffice uses the print settings stored in your file. If you set the page layout to landscape, LibreOffice prints landscape. If you set it to fit all columns on one page, LibreOffice does so. If you set the print area to a specific range, LibreOffice only prints this range. That gives you a handy way to control the output.

Summary: parsing documents with GPT-4o

GPT-4o, one of the most capable models with vision input at the time of writing, solves a long-standing problem: extracting data from complex documents such as PDFs, Word files and Excel spreadsheets.

Converting documents into images and letting GPT-4o's visual understanding do the reading gives very high accuracy in text extraction and document parsing. It works especially well for multi-column layouts, tables and embedded images or charts, which have traditionally been hard for conventional parsing methods.

To be fair, the pipeline looks odd: we open a file, convert it to PDF, convert that to an image, send the image to a multi-billion-parameter model, ask it nicely to extract the text and finally get the text back. But the results speak for themselves. They are miles ahead of what was there before.

To recap: the same image-based approach covers PDFs, Word documents and Excel spreadsheets. DOCX and XLSX files, which have no inherent pages, just need a PDF conversion with LibreOffice first. Prompts let you control what gets extracted and how it is formatted. And the cost is modest, at about 0.32 to 0.62 cents per page for most documents. To try it, start with the PDF code above on a handful of your own documents and compare the output with your current parser.

Customer orders often arrive in exactly these three formats. For that case we built Pondhouse Orderdesk, which reads order emails, PDFs and Excel lists and turns them into orders in the ERP system.

Further reading

Interested in building high-quality AI agent systems?

We prepared a comprehensive guide based on cutting-edge research for how to build robust, reliable AI agent systems that actually work in production. This guide covers:

  • Understanding the 14 systematic failure modes in multi-agent systems
  • Evidence-based best practices for agent design
  • Structured communication protocols and verification mechanisms
Andreas Nigg

About the author

Andreas Nigg

Co-Founder, Pondhouse Data

Andreas has spent more than eight years building data applications. At Pondhouse Data he designs and ships AI systems and integrates them into complex, real-world environments.