在Java(Eclipse)中使用Python脚本

6 投票
1 回答
3834 浏览
提问于 2025-04-16 20:06

我一直想把一个朋友为我写的Python脚本放到我正在开发的Java应用程序里。经过一番尝试,我终于发现了“Jython”,并用PythonInterpreter来运行这个脚本。

但是,当我尝试运行它时,脚本里出现了一个错误。这很奇怪,因为当我在Java之外运行这个脚本(在Eclipse IDE中)时,它工作得很好,完全能满足我的需求(从同一目录下的.docx文件中提取所有图片)。

有没有人能帮我解决这个问题?

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();
        }
    }
}

关于Python脚本的Java错误:

追踪记录(最近的调用在最前面):
文件 "C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py",第19行, thisDir,_ = path.split(path.abspath(argv[0])) IndexError: 索引超出范围:0 追踪记录(最近的调用在最前面):
文件 "C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/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

#A few notes -
#(1) when I do something like " _,variable = something ", that is because
#the function returns two variables, and I only need one.  I don't know if it is a
#common convention to use the '_' symbol as the name for the unused variable, but
#I saw it in some guy's code in the past, and I started using it.
#(2) I use "path.join" because on unix operating systems and windows operating systems
#they use different conventions for paths like '\' vs '/'.  path.join works on all operating
#systems for making paths.

#Defines what extensions to look for within the file (you can add more to this)
IMAGE_FILE_EXTENSIONS = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tif', '.tiff')

#Changes to the directory in which this script is contained
thisDir = getcwd()
chdir(thisDir)

#Lists all the files/folders in the directory
fileList = listdir('.')
for file in fileList:

    #Checks if the item is a file (opposed to being a folder)
    if path.isfile(file):

        #Fetches the files extension and checks if it is .docx
        _,fileExt = path.splitext(file)
        if fileExt == '.docx':

            #Creates directory for the images
            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)



            #Opens the file as if it is a zipfile
            #Then lists the contents
            try:
                zipFileHandle = ZipFile(file)
                nameList = zipFileHandle.namelist()

                for archivedFile in nameList:
                    #Checks if the file extension is in the list defined above
                    #And if it is, it extracts the file
                    _,archiveExt = path.splitext(archivedFile)
                    if archiveExt in IMAGE_FILE_EXTENSIONS:
                        zipFileHandle.extract(archivedFile, newDirectory)
            except:
                pass

1 个回答

2

我猜如果直接调用解释器的话,你是拿不到命令行参数的(这也不奇怪,毕竟它从哪里获取正确的值呢?[或者说,什么才是正确的值呢?])。

os.getcwd()

Return a string representing the current working directory.

这个命令会返回当前工作目录,但我想这不是你想要的结果。

虽然没有测试过,但我觉得用 os.path.dirname(os.path.realpath(__file__)) 应该可以实现(注意:中间的空格要去掉;我应该找时间仔细看看格式选项~)

撰写回答