1

我正在开发一个基于 java 的项目。并且java程序将运行命令来调用python脚本。

python脚本用于tabula-py读取pdf文件并返回数据。

当我在终端(pytho3 xxx.py)中直接调用它时,我尝试了 python 脚本的工作

但是,当我尝试从 java 调用 python 脚本时,它会抛出错误:

Error from tabula-java:Error: File does not exist
Command '['java', '-Dfile.encoding=UTF8', '-jar', '/home/ubuntu/.local/lib/python3.8/site-packages/tabula/tabula-1.0.5-jar-with-dependencies.jar', '--pages', 'all', '--lattice', '--guess', '--format', 'JSON', '/home/ubuntu/Documents/xxxx.pdf']' returned non-zero exit status 1.

我试图以完整路径调用脚本,以完整路径提供pdf文件,尝试过sys.append(python script path),但它们都不起作用。

我试过在java命令中调用tabula,即java -Dfile.encoding=UTF8 -jar /home/ubuntu/.local/lib/python3.8/site-packages/tabula/tabula-1.0.5-jar-与-dependencies.jar “file_path”

它可以工作并且可以读取文件。然而回到java调用python脚本是行不通的

有什么方法可以解决这个问题吗?在我的情况下,在 java 程序中使用表格不是一个选项

4

1 回答 1

0

既然您提到您提到您使用 java 编写基本代码,使用 python 读取 PDF,最好完全使用 java 来获得更高效的代码。为什么?因为已经为您准备好了工具。绝对没有必要努力将一种语言与另一种语言联系起来。

代码:


import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

/**
 * This class is used to read an existing
 *  pdf file using iText jar.
 */
public class PDFReadExample {
    public static void main(String args[]){
        try {
            //Create PdfReader instance.
            PdfReader pdfReader = new PdfReader("D:\\testFile.pdf");    
            
            //Get the number of pages in pdf.
            int pages = pdfReader.getNumberOfPages(); 
            
            //Iterate the pdf through pages.
            for(int i=1; i<=pages; i++) { 
                //Extract the page content using PdfTextExtractor.
                String pageContent = 
                    PdfTextExtractor.getTextFromPage(pdfReader, i);
                
                //Print the page content on console.
                System.out.println("Content on Page "
                              + i + ": " + pageContent);
            }
            
            //Close the PdfReader.
            pdfReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
于 2021-11-29T04:34:27.333 回答