textract Documentation
repository·master·Indexed 26 days ago
https://github.com/deanmalmgren/textractA 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.
What's inside textract
- textract is a Python library designed to extract text from various document formats with minimal configuration. It aims to provide a simple interface for text extraction across many different file types.
Install textract on macOS
masterInstallation on macOS requires
Homebrew. You must install XQuartz and system packages first, followed by the Python package.Note:
libreofficeis optional (only for legacy.docfiles).ghostscriptis required for.psfile 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
RuntimeErrorregarding an invalid code signature when usingmethod="sphinx", you must manually re-sign thepocketsphinxextension 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 textractUse the textract CLI
masterThetextractcommand-line interface is provided via thetextract.climodule. It usesargparseto handle command-line arguments. To enable shell autocompletion for available options, install and activate theargcompletepackage.Install textract using modern Python tooling (uv)
masterYou can use
uvto runtextractas a one-off command without permanent installation, or install it as a global tool.One-off execution: Use
uvxto run the command immediately.Install as tool: Use
uv tool installto maketextractavailable globally.# One-off execution uvx textract path/to/file.pdf # Install as tool uv tool install textractImplement a custom parser by inheriting from BaseParser
masterTo add support for a new file extension, create a module namedtextract.parsers.extension_parsercontaining aParserclass. ThisParserclass must inherit fromtextract.parsers.utils.BaseParser.Convert legacy .doc files to .docx for textract
masterIf you want to avoid installing the heavy
LibreOfficedependency (e.g., in a slim container), you can pre-convert legacy.doc(Word 97-2003) files to.docxusingsoffice. The.docxparser intextractis pure Python and does not require LibreOffice.Use the following command to batch convert files:
soffice --headless --convert-to docx --outdir out/ *.docsoffice --headless --convert-to docx --outdir out/ *.docInstall textract on FreeBSD
masterInstall the required system packages using
pkg, then install the Python package viapiporuv.Note:
editors/libreofficeis optional and only required for legacy.docfiles.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 textractInstall textract on Ubuntu / Debian
masterTo install
textracton Ubuntu or Debian, you must first install the required system libraries viaapt-get, then install the Python package usingpiporuv.Note:
libreoffice-writeris 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 installzlib1g-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 textractInstall textract on Windows
masterInstall
Chocolateyfirst, then install the required system packages. Finally, installtextractviapiporuv.Limitations on Windows:
.mp3/.oggfiles: Not supported becausesox.portabledoes not includelibmad. Use Linux or macOS for audio extraction..rtffiles: Not supported becauseunrtfhas 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 textractExtract text from a file using textract.process()
masterThe primary way to use
textractis by callingtextract.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')Use the textract CLI to extract text from files
masterThetextractcommand-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.Re-sign pocketsphinx extension on macOS (Python 3.14+)
masterIf you encounter a
RuntimeErrormentioning an invalid code signature when usingmethod="sphinx"on macOS ARM64 with Python 3.14+, run this Python command to re-sign thepocketsphinxextension: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}') "