PyxTxt
PyxTxt is a small Python library that extracts plain text from many file formats through a single function, xtxt().
It detects the file type automatically (via libmagic) and dispatches to the right extractor. Extractors are optional:
install only the ones you need.
from pyxtxt import xtxt
text = xtxt("report.pdf")
✨ Features
- One function for everything:
xtxt()accepts a file path, a binary file object, anio.BytesIObuffer, rawbytesor arequests.Response - Automatic type detection with
python-magic, refined by the file extension when libmagic is not specific enough (e.g. Markdown) - Modular dependencies: each format is an optional extra, the core only needs
python-magic - Office, web and document formats: PDF, DOCX, PPTX, XLSX, XLS, ODT, HTML, XML, SVG, Markdown, EPUB, RTF, EML, MSG, LaTeX, DOC, TXT
- Audio and video transcription with OpenAI Whisper
- OCR from images with EasyOCR or with a local multimodal LLM through Ollama
- EXIF metadata extraction from photos
📄 Supported formats
| Format | Install extra | Notes |
|---|---|---|
pdf |
PyMuPDF | |
| DOCX | docx |
Paragraphs and tables in document order, headers and footers |
| PPTX | presentation |
Text boxes, grouped shapes, tables and speaker notes |
| XLSX, XLS | spreadsheet |
Every row of every visible sheet, cells joined with | |
| ODT | odf |
|
| HTML | html |
|
| XML, SVG | html |
Both use lxml, installed by the html extra |
| Markdown | markdown |
Detected by the .md / .markdown extension |
| EPUB | epub |
|
| RTF | rtf |
|
| EML | email |
Plain-text and HTML parts |
| MSG (Outlook) | outlook |
Plain-text body, or the HTML body when there is no plain text |
| LaTeX | latex |
|
| DOC (legacy Word) | — | Needs the antiword system tool |
TXT and other text/* |
— | Always available, decoded as UTF-8 |
| Audio and video | audio |
Whisper, needs ffmpeg; heavy download. MP3, WAV, M4A, AAC, FLAC, OGG/Opus, AIFF, WMA, MP4, MOV, AVI, MKV, WebM |
| Images (OCR) | ocr or ocr-ollama |
See OCR from images |
To list what is available in your installation:
from pyxtxt import extxt_available_formats
print(extxt_available_formats()) # MIME types
print(extxt_available_formats(pretty=True)) # Short names
📦 Installation
PyxTxt requires Python 3.10 or newer.
Install every extractor (this includes the heavy audio and OCR dependencies):
pip install "pyxtxt[all]"
or only the formats you need:
pip install "pyxtxt[pdf,docx,presentation,spreadsheet,html,markdown,epub,email]"
Heavy optional extras:
pip install "pyxtxt[audio]" # Whisper transcription (~2 GB with models, pulls in PyTorch)
pip install "pyxtxt[ocr]" # EasyOCR (~1 GB with models, pulls in PyTorch)
pip install "pyxtxt[ocr-ollama]" # OCR through a local Ollama server
System dependencies
libmagic (required by python-magic):
sudo apt install libmagic1 # Ubuntu / Debian
brew install libmagic # macOS
On Windows the python-magic-bin package, which bundles libmagic, is installed automatically.
antiword (only for legacy .doc files):
sudo apt install antiword # Ubuntu / Debian
brew install antiword # macOS
ffmpeg (only for audio/video transcription):
sudo apt install ffmpeg # Ubuntu / Debian
brew install ffmpeg # macOS
# Windows: https://ffmpeg.org/download.html
📚 Usage
Basic usage
import io
from pyxtxt import xtxt
# From a file path
text = xtxt("document.pdf")
# From a file object opened in binary mode
with open("document.docx", "rb") as f:
text = xtxt(f)
# From an in-memory buffer
buffer = io.BytesIO(docx_bytes)
text = xtxt(buffer)
# Give the buffer a name to help type detection (useful for Markdown, LaTeX, RTF)
buffer = io.BytesIO(markdown_bytes)
buffer.name = "notes.md"
text = xtxt(buffer)
xtxt() returns the extracted text as a str, or None when the file cannot be read or its type is not supported.
Web content
xtxt_from_url() and requests.Response support need the requests package (pip install requests).
import requests
from pyxtxt import xtxt, xtxt_from_url
response = requests.get("https://example.com/document.pdf")
text = xtxt(response.content) # from bytes
text = xtxt(response) # from the Response object
text = xtxt_from_url("https://example.com/document.pdf", timeout=10)
Extra keyword arguments of xtxt_from_url() are passed to requests.get().
Typical uses:
# File uploads (Flask / Django)
text = xtxt(request.files["document"].read())
# Email attachments
text = xtxt(attachment.get_payload(decode=True))
Audio and video transcription
from pyxtxt import xtxt
text = xtxt("meeting_recording.mp3")
text = xtxt("interview.wav")
text = xtxt("presentation.mp4") # the audio track is extracted automatically
The Whisper base model is downloaded on first use and cached for the following calls.
🖼 OCR from images
Two OCR back-ends are available. If both are installed, Ollama takes precedence for xtxt() on images.
EasyOCR (pip install "pyxtxt[ocr]") runs locally on CPU, recognising Italian and English:
text = xtxt("scanned_document.png")
Ollama (pip install "pyxtxt[ocr-ollama]") uses a multimodal LLM served by a local
Ollama instance. Start the server and pull a model first (ollama pull gemma3:4b).
from pyxtxt import (
xtxt, xtxt_image_describe,
set_ollama_model, set_ollama_config, get_ollama_config, reset_ollama_config,
)
set_ollama_model("gemma3:12b") # default: gemma3:4b; also llava:7b, llava:13b, gemma3:27b
set_ollama_config(
language="italian", # language hint
caption_length="long", # short, medium, long
style="detailed", # descriptive, technical, simple, detailed
context="document", # general, document, handwriting, technical, cookbook, ...
temperature=0.2,
max_tokens=2000,
auto_fallback=False, # by default other models are tried when the result looks poor
)
text = xtxt("complex_document.png") # text only
analysis = xtxt_image_describe("scientific_diagram.png")
# TEXT: ...
# DESCRIPTION: ...
print(get_ollama_config())
reset_ollama_config()
Confidence score
from pyxtxt import xtxt_image_with_confidence, set_ollama_config
set_ollama_config(confidence_threshold=0.8)
text, confidence = xtxt_image_with_confidence("document.png", mode="ocr")
The score is a heuristic computed on the model's answer: it rewards structured text (numbers, punctuation) and
penalises vague language and typical hallucination keywords (e.g. "ancient", "papyrus", "painting", "dragon").
It is not a calibrated probability. In OCR mode, results below confidence_threshold (default 0.7) are discarded
and an empty string is returned.
EXIF metadata
Requires Pillow (installed by the ocr or ocr-ollama extras, or pip install pillow).
from pyxtxt import xtxt_exif
print(xtxt_exif("vacation_photo.jpg"))
# Camera make/model, shooting settings, date/time, GPS coordinates, image size...
More examples
An examples script is installed with the package:
python -m pyxtxt.examples
or, to read its source:
from importlib.resources import files
print((files("pyxtxt") / "examples.py").read_text())
⚠️ Known limitations
- Supported inputs: file paths, binary file objects,
io.BytesIO,bytesandrequests.Response. File objects opened in text mode are rejected: open them with"rb". - Type detection without a file name: libmagic cannot tell apart some formats from raw bytes (legacy Office files
share the same signature; Markdown looks like plain text). Pass a file path, or set
buffer.name, when possible. - Legacy PowerPoint (
.ppt) is not supported. - DOCX: text boxes, footnotes and comments are not extracted. PPTX: text inside charts is not extracted.
- Errors are reported with messages printed to standard output and the functions return
Noneor an empty string; they do not raise exceptions.
🤖 AI-powered features
OCR through Ollama and Whisper transcription rely on machine-learning models and can produce hallucinations (text or content that is not there), misinterpretations and language errors. Results vary between models and versions.
Do not use them for critical applications — medical diagnosis or medical image interpretation, legal or financial documents where errors matter, safety systems — without human verification. Validate the results against the source, use the confidence score as a hint only, and keep a traditional OCR (EasyOCR) as a cross-check when accuracy matters.
🛠 Development
git clone https://github.com/GiuseppeLeviBo/pyxtxt
cd pyxtxt
python -m venv .venv && source .venv/bin/activate
pip install -e ".[pdf,docx,presentation,spreadsheet,odf,html,markdown,epub,rtf,email,latex]" pytest
pytest
Tests for formats whose libraries are not installed are skipped. CI runs the test suite on Python 3.10–3.14.
Releasing
Releases are published to PyPI by GitHub Actions (.github/workflows/publish.yml) through
Trusted Publishing, so no API token is needed:
- Update
versioninpyproject.tomland the changelog below; merge tomain. - Tag and push:
git tag v0.3.6 && git push origin v0.3.6.
The workflow checks that the tag matches the version, builds sdist and wheel, and uploads them.
One-time setup: on PyPI, open the project's Settings → Publishing and add a
GitHub publisher with owner GiuseppeLeviBo, repository pyxtxt, workflow publish.yml and environment pypi.
🔒 License
Distributed under the MIT License. See LICENSE.
🤝 Contributing
Pull requests, issues and feedback are welcome.
- Bug reports: include a sample file (or how to create one) and the full error message
- Feature requests: describe your use case and the expected behaviour
- Code: follow the existing patterns and add a test in
tests/
📊 Changelog
v0.3.8
- NEW:
xtxt()accepts binary file objects, e.g.xtxt(open("file.pdf", "rb")) - FIXED: XLSX passed as
bytesorBytesIOwas detected as a ZIP archive and rejected - FIXED: WAV, M4A, AAC, AIFF, WMA and MKV files were never transcribed (libmagic reports them as
audio/x-wav,audio/x-m4a,video/x-matroska, ... which were not registered) - FIXED: MSG extraction always failed (
extract_msg.Messagehas noextract()method) - FIXED: SVG extraction failed on
<text>elements containing<tspan> - IMPROVED: DOCX now includes tables (in document order), headers and footers
- IMPROVED: PPTX now includes grouped shapes, tables and speaker notes
- IMPROVED: ODT now includes headings and text inside spans, links and lists
- IMPROVED: EXIF output formats aperture, shutter speed and focal length as intended and reads PNG/WebP metadata
through Pillow's public API; the fake
image/*+exifMIME types are gone fromextxt_available_formats() - IMPROVED: Whisper temporary files are always deleted, also when transcription fails
- IMPROVED: Ollama OCR stops trying fallback models when the server is not reachable;
xtxt_image_with_confidence()now uses the same context-aware prompts asxtxt(); hallucination keywords match whole words (e.g. "roman" no longer fires on "romance")
v0.3.7
- Python 3.10 or newer is now required; metadata no longer lists the end-of-life versions 3.7–3.9, which were never tested
- Python 3.14 is tested in CI and declared as supported
v0.3.6
- FIXED:
import pyxtxtcrashed withAttributeErrorunless bothollamaand Pillow were installed (regression in 0.3.4.2 and 0.3.5) - FIXED: Markdown, RTF and LaTeX files were returned as raw source instead of being converted to text
- FIXED: a failed extraction (e.g. a corrupted PDF) returned the string
"None"instead ofNone - FIXED: XLSX and XLS files were silently truncated to 200 and 100 rows per sheet; all rows are now extracted
(
max_rows_per_sheetis still available when calling the extractors directly) - FIXED: when both EasyOCR and Ollama were installed, the OCR back-end used for images was random; Ollama now always takes precedence
- FIXED: a single extractor failing to load no longer prevents the whole package from importing
- FIXED:
BytesIObuffers keep theirname, which is now used to refine type detection - PyMuPDF is imported as
pymupdf, removing the deprecation warning printed at every import - Added the missing
LICENSEfile, SPDX license metadata and project URLs - Removed the obsolete
pyxtxt/pyxtxt.pymodule - Added a test suite and GitHub Actions workflows for CI and PyPI publishing
v0.3.0 – v0.3.5
- NEW: OCR through Ollama multimodal models (
set_ollama_model,xtxt_image_describe) — 0.3.0 - NEW:
set_ollama_config(),get_ollama_config(),reset_ollama_config()— 0.3.2 - NEW: confidence score and hallucination detection (
xtxt_image_with_confidence) — 0.3.4 - NEW: image enhancement before OCR, automatic model fallback, context presets — 0.3.4.2
- NEW: EXIF metadata extraction (
xtxt_exif) — 0.3.5
v0.2.4
- NEW: video transcription support (MP4, MOV, AVI, WebM, MKV) via Whisper
v0.2.3
- NEW: audio transcription (MP3, WAV, M4A, FLAC, ...) with Whisper
- NEW: OCR from images (JPEG, PNG, TIFF, BMP, WebP) with EasyOCR
- NEW:
audio,ocrandallinstallation extras
v0.2.0 – v0.2.2
- NEW: automatic extractor registration
- NEW: Markdown, EPUB, RTF, EML, MSG and LaTeX extractors
v0.1.24
- NEW: support for
bytesandrequests.Responseinputs,xtxt_from_url()helper
v0.1.0 – v0.1.23
- Initial releases: modular extractors for PDF, DOCX, PPTX, XLSX, ODT, HTML, XML, TXT and legacy Office files,
MIME detection with python-magic,
BytesIOsupport
Metadata
Release files for pyxtxt 0.3.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyxtxt-0.3.8.tar.gz | 42.8 kB | With provenance | Details for pyxtxt-0.3.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyxtxt-0.3.8-py3-none-any.whl | Python 3 | none | any | Details for pyxtxt-0.3.8-py3-none-any.whl |
Total release size: 80.9 kB
Release files / pyxtxt-0.3.8.tar.gz
| Download URL | pyxtxt-0.3.8.tar.gz |
|---|---|
| Size | 42.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1605f7412a38925baa6db0f7d8ddb02dd3d0dfab08d5f7900ed9cf09c79269f9
|
|
BLAKE2b-256 checksum How to use checksums |
901269b9a7c9e633fd87f4c397054c8112e69d824f9c540f56642f7b7bb37f7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logRelease files / pyxtxt-0.3.8-py3-none-any.whl
| Download URL | pyxtxt-0.3.8-py3-none-any.whl |
|---|---|
| Size | 38.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0c884c8b73a199c38f596d04dd770498978d3c985b6633a9f9cebb44dab43481
|
|
BLAKE2b-256 checksum How to use checksums |
05d74a8391d4f318ff75ce1249fe89c29b6efe0d5cfed4c7381ab4af561288be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logSecurity for pyxtxt 0.3.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pyxtxt-0.3.8.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.