I was looking for the possibility of adding a Python Script friend made for me to the Java application that I am trying to develop. After some trial and error, I finally found out about "Jython" and used PythonInterpreter to try to run the script.
However, trying to run it, I get an error message in a Python script. This is strange because when I try to run Script outside of Java (the Eclipse IDE in this case), Script works fine and does exactly what I need (extract all the images from the .docx files stored in its same directory).
Can anyone help me here?
Java:
import org.python.core.PyException;
import org.python.util.PythonInterpreter;
public class SPImageExtractor
{
public static void main(String[] args) throws PyException
{
try
{
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py");
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}
Java error regarding Python Script:
Traceback ( ):
"C:/Documents and // /Intern //Converted /Image -Extractor2.py", 19, thisDir, _ = path.split(path.abspath(argv [0])) IndexError: : 0 Traceback ( ):
"C:/Documents and // /Intern //Converted /Image -Extractor2.py", 19, thisDir, _ = path.split(path.abspath(argv [0])) IndexError: : 0
Python:
from os import path, chdir, listdir, mkdir, gcwd
from sys import argv
from zipfile import ZipFile
from time import sleep
IMAGE_FILE_EXTENSIONS = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tif', '.tiff')
thisDir = getcwd()
chdir(thisDir)
fileList = listdir('.')
for file in fileList:
if path.isfile(file):
_,fileExt = path.splitext(file)
if fileExt == '.docx':
newDirectory = path.join(thisDir, file + "-Images")
if not path.exists(newDirectory):
mkdir(newDirectory)
currentFile = open(file,"r")
for line in currentFile:
print line
sleep(5)
try:
zipFileHandle = ZipFile(file)
nameList = zipFileHandle.namelist()
for archivedFile in nameList:
_,archiveExt = path.splitext(archivedFile)
if archiveExt in IMAGE_FILE_EXTENSIONS:
zipFileHandle.extract(archivedFile, newDirectory)
except:
pass