簡體   English   中英

當 java 調用 python 腳本時,tabula-py 無法讀取文件

[英]tabula-py can't read file when the python script called by java

我正在開發一個基於 java 的項目。 java 程序將運行命令調用 python 腳本。

python 腳本用於 tabula-py 讀取 pdf 文件並返回數據。

我嘗試了 python 腳本在我直接在終端中調用它時有效(pytho3 xxx.py)

但是,當我嘗試從 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 命令中調用表格,即 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 程序中的表格不適合我的情況

Now that you mention that you mention you use java for base code and python for reading PDF, It's better of using java entirely for more efficient code. 為什么? 因為已經為您准備好了工具。

代碼:


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM