Skip to main content

PyTika

Workflow status PyPi wheel

An Apache Tika Server python client.

Installation

You can install the package simply from pypi: pip install pytika

Usage

This package is a python client for Apache Tika Server, so you'll need to have Tika Server running locally somehow. I would recommend using the docker image as that's the simplest. docker run --name tika-server -it -d -p 9998:9998 apache/tika:2.7.0-full

After you have that running, you can use PyTika to interface with it.

Metadata queries

from pytika.api import TikaApi

tika = TikaApi()
metadata = api.get_meta(file)

"""
>>> metadata
{
    'Content-Type': 'application/pdf',
    ...
}
"""

Text detection queries

For text detection, Tika Server usually decides on the response type (typically xml/html is the default). To force it to return plain text (Accept: text/plain header) you can set the following configuration:

from pytika.api import TikaApi
from pytika.config import GetTextOptionsBuilder as opt

tika = TikaApi()

with open("yourfile.whatever", "rb") as file:
    text = api.get_text(file, opt.AsPlainText()).decode()

Notice the awkward configuration - passing a function call as an option - this is coming from a nice Golang standard that makes calling complex APIs a little friendlier. Since we have a lot of options, instead of having each be an argument, we can define an "option class" with chainable functions. This allows the API to validate each separately, avoid having a massive list of arguments for get_text, as well as tidy up the API code. (For more info: Uptrace, Dave Cheney's post)

For detection in HOCR format with bounding boxes:

from pytika.api import TikaApi
from pytika.config import GetTextOptionsBuilder as opt

tika = TikaApi()

with open("yourfile.whatever", "rb") as file:
    text = api.get_text(file, opt.WithBoundingBoxes()).decode()

There are many more configuration options that you can look into in the GetTextOptionsBuilder class, and more to come in the future.

Contribution Guide

If you'd like to add some missing features that you can find in TikaServer or Tika, then you can contribute to this repo yourself!

1- Clone the repository

git clone "url from repo, either ssh or https"

2- Create a branch

cd pytika
git switch -c your-new-branch-name

3- Make necessary changes and commit, and push to Github

git add README.md
git commit -m "Updated README.md with new API changes"
git push -u origin your-new-branch-name

4- Go to your repository and you'll see a Compare and pull request button, click on that.

5- Wait for us to review your PR, likely leave comments, and hopefully merge it in!

Metadata

Release files for pytika 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytika 0.2.0
File Size Uploaded
pytika-0.2.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytika 0.2.0
File Interpreter ABI Platform
pytika-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.4 kB

Release files / pytika-0.2.0.tar.gz

Download URL pytika-0.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4c473945da5d6d97cea1aab9c7156e57d1054dfb64c7c5597f326c6a26590b62
BLAKE2b-256 checksum
How to use checksums
f1a3c10538eeb6ce49f5ea08c87c55c1d7aab66e59d1c5ec28fb582324d9f7fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Darwin/22.2.0

Release files / pytika-0.2.0-py3-none-any.whl

Download URL pytika-0.2.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52376167b0f0cb252f71413f03e2dd673e68c5318bfa122b17ce3881ac96a483
BLAKE2b-256 checksum
How to use checksums
bc701eef6755ca31948478b8754050e733b723ff18b8ee1d958e52ed5bc4b934
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page