textract Documentation

repository·master·Indexed 26 days ago

https://github.com/deanmalmgren/textract

A Python library and CLI tool designed to extract raw text from various document formats for natural language processing tasks. It provides a unified interface via the textract.process() method and a command-line interface to handle a wide range of file types, supporting custom parsers and specialized options for OCR and layout preservation.

Tokens
3.1K
Snippets
11
Records
30
Agent score
89%

What's inside textract

  1. Install textract on macOS

    master

    Installation on macOS requires Homebrew. You must install XQuartz and system packages first, followed by the Python package.

    Note:

    • libreoffice is optional (only for legacy .doc files).
    • ghostscript is required for .ps file extraction.
    • You may need to install Python development header files depending on your Homebrew configuration.
    • Python 3.14+ on macOS ARM64: If you encounter a RuntimeError regarding an invalid code signature when using method="sphinx", you must manually re-sign the pocketsphinx extension using the provided Python script workaround.
    brew install --cask xquartz libreoffice
    brew install ghostscript poppler sox tesseract unrtf swig
    pip install textract
    # or with uv
    uv pip install textract
  2. Install textract using modern Python tooling (uv)

    master

    You can use uv to run textract as a one-off command without permanent installation, or install it as a global tool.

    One-off execution: Use uvx to run the command immediately.

    Install as tool: Use uv tool install to make textract available globally.

    # One-off execution
    uvx textract path/to/file.pdf
    
    # Install as tool
    uv tool install textract
  3. Convert legacy .doc files to .docx for textract

    master

    If you want to avoid installing the heavy LibreOffice dependency (e.g., in a slim container), you can pre-convert legacy .doc (Word 97-2003) files to .docx using soffice. The .docx parser in textract is pure Python and does not require LibreOffice.

    Use the following command to batch convert files:

    soffice --headless --convert-to docx --outdir out/ *.doc

    soffice --headless --convert-to docx --outdir out/ *.doc
  4. Install textract on FreeBSD

    master

    Install the required system packages using pkg, then install the Python package via pip or uv.

    Note: editors/libreoffice is optional and only required for legacy .doc files.

    pkg install lang/python38 devel/py-pip textproc/libxml2 textproc/libxslt editors/libreoffice textproc/unrtf \    graphics/poppler print/pstotext graphics/tesseract audio/flac multimedia/ffmpeg audio/lame audio/sox \    graphics/jpeg-turbo
    pip install textract
    # or with uv
    uv pip install textract
  5. Install textract on Ubuntu / Debian

    master

    To install textract on Ubuntu or Debian, you must first install the required system libraries via apt-get, then install the Python package using pip or uv.

    Note: libreoffice-writer is optional and only required if you need to extract legacy .doc (Word 97-2003) files. On Docker instances of Ubuntu, you may also need to install zlib1g-dev.

    apt-get install python-dev libxml2-dev libxslt1-dev libreoffice-writer unrtf poppler-utils ghostscript tesseract-ocr \    flac ffmpeg lame libmad0 libsox-fmt-mp3 sox libjpeg-dev swig libpulse-dev
    pip install textract
    # or with uv
    uv pip install textract
  6. Install textract on Windows

    master

    Install Chocolatey first, then install the required system packages. Finally, install textract via pip or uv.

    Limitations on Windows:

    • .mp3 / .ogg files: Not supported because sox.portable does not include libmad. Use Linux or macOS for audio extraction.
    • .rtf files: Not supported because unrtf has no Windows port. RTF extraction is only available on Linux and macOS.
    choco install tesseract ghostscript sox.portable poppler libreoffice-fresh -y
    pip install textract
    # or with uv
    uv pip install textract
  7. Extract text from a file using textract.process()

    master

    The primary way to use textract is by calling textract.process(path_to_file). This function automatically detects the file extension and uses the appropriate parser to return the document's text content.

    import textract
    text = textract.process('path/to/file.extension')
  8. Use the textract CLI to extract text from files

    master
    The textract command-line tool allows you to extract text from various document formats. The basic usage requires providing a filename. You can specify output encoding, file extensions, extraction methods, and output destinations.
  9. Re-sign pocketsphinx extension on macOS (Python 3.14+)

    master

    If you encounter a RuntimeError mentioning an invalid code signature when using method="sphinx" on macOS ARM64 with Python 3.14+, run this Python command to re-sign the pocketsphinx extension:

    import subprocess
    from pathlib import Path
    import importlib.util
    spec = importlib.util.find_spec('pocketsphinx')
    for loc in (spec.submodule_search_locations or []):
        for so in Path(loc).glob('_pocketsphinx.cpython-*-darwin.so'):
            subprocess.run(['codesign', '-s', '-', '-f', str(so)], check=True)
            print(f'Re-signed: {so}')
    python -c "
    import subprocess
    from pathlib import Path
    import importlib.util
    spec = importlib.util.find_spec('pocketsphinx')
    for loc in (spec.submodule_search_locations or []):
        for so in Path(loc).glob('_pocketsphinx.cpython-*-darwin.so'):
            subprocess.run(['codesign', '-s', '-', '-f', str(so)], check=True)
            print(f'Re-signed: {so}')
    "