问题
I'm trying to parse a few PDF files that contain engineering drawings to obtain text data in the files. I tried using TIKA as a jar with python and using it with the jnius package (using this tutorial here: http://www.hackzine.org/using-apache-tika-from-python-with-jnius.html) but the code throws an error.
Using the TIKA package however I was able to pass files and parse them but Python is only able to extract metadata and when asked to parse content, Python returns output "none". It is able to perfectly parse .txt files but fails for content extraction for PDFs. Here's the code
import tika
tika.initVM()
from tika import parser
parsed = parser.from_file('/path/to/file')
print parsed["metadata"]
print parsed["content"]
Do I require additional packages/codelines to be able to extract the data?
回答1:
You need to download the Tika Server Jar and run it first. Check this link: http://wiki.apache.org/tika/TikaJAXRS
- Download the Jar
- Store it somewhere and run it as
java -jar tika-server-x.x.jar --port xxxx - In your Code you now don't need to do the
tika.initVM()Addtika.TikaClientOnly = Trueinstead oftika.initVM() - Change
parsed = parser.from_file('/path/to/file')toparsed = parser.from_file('/path/to/file', '/path/to/server')You will get the server path in Step 2. when the tika server initiates - just plug that in here
Good luck!
回答2:
can you please share the file you are looking at? The easiest way to do this would be to perhaps attach it to a Github issue in my repository, etc.
That said, if you are trying to use OCR and Tika, you need to run through the Tika OCR guide (http://wiki.apache.org/tika/TikaOCR) and get Tesseract installed. Once Tesseract is installed, then you need to double check whether or not you have an instance of tika-server running (e.g., ps aux | grep tika). If you do, kill it (tika-python runs the Tika REST server in the background as its main interface to Tika; having a fresh running version of it after Tesseract OCR is installed helps to eliminate any odd possibilities).
After you have Tesseract OCR installed, no tika-server running, start your python2.7 interpreter (or script), and then do something like:
from tika import parser
parsed = parser.from_file('/path/to/file')
print parsed["content"] # should be the text returned from OCR
HTH! --Chris
回答3:
I never tried python tikq , but pyjnius is working fine for me. Here is my code:
def parse_file(filename):
"""
Import TIKA classes and parse input filename
"""
import os
os.environ['CLASSPATH'] = "/path/to/tika-app.jar"
from jnius import autoclass
from jnius import JavaException
# Import the Java classes
Tika = autoclass('org.apache.tika.Tika')
Metadata = autoclass('org.apache.tika.metadata.Metadata')
FileInputStream = autoclass('java.io.FileInputStream')
tika = Tika()
tika.setMaxStringLength(10*1024*1024);
meta = Metadata()
# Raise an exception and continue if parsing fails
try:
text = tika.parseToString(FileInputStream(filename), meta)
return text
except (JavaException,UnicodeDecodeError), e:
print "ERROR: %s" % (e)
return None
来源:https://stackoverflow.com/questions/33073972/how-can-i-use-tika-packagehttps-github-com-chrismattmann-tika-python-in-pyth