You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin项目中Chaquopy调用tabula-py遇JavaNotFoundError求助

解决方案:Chaquopy安卓项目中调用PDF表格提取工具避免JavaNotFoundError

问题根源

tabula-py本质是通过调用系统中的java命令执行底层tabula-java jar包,但安卓设备没有预装标准JVM的java可执行文件,且开发机上的JAVA_HOME/PATH配置仅对本地进程生效,无法传递到安卓设备的Chaquopy运行环境中,因此无论在开发机如何配置都无法解决该错误。

可行方案

方案1:替换为纯Python PDF表格提取库(推荐)

使用不依赖Java的纯Python库pdfplumber,适配Chaquopy安卓环境:

  1. 添加依赖:在app模块的build.gradle中配置Chaquopy的pip依赖:
chaquopy {
    pip {
        install "pdfplumber"
        install "pdfminer.six"  // pdfplumber的依赖库
    }
}
  1. 修改Python脚本:
import pdfplumber
from io import BytesIO

def main(pdf_data):
    tables = []
    with pdfplumber.open(BytesIO(pdf_data)) as pdf:
        for page in pdf.pages:
            # 提取当前页面所有表格,可通过参数调整识别规则
            page_tables = page.extract_tables()
            tables.extend(page_tables)
    return str(tables)

方案2:直接在Kotlin中调用tabula-java

绕开Python的tabula-py,直接在安卓原生代码中调用tabula-java处理PDF:

  1. 添加依赖:在app模块的build.gradle中引入tabula-java:
dependencies {
    implementation 'technology.tabula:tabula:1.0.5'
}
  1. 编写Kotlin处理代码:
import technology.tabula.ObjectExtractor
import technology.tabula.Page
import java.io.ByteArrayInputStream

fun extractPdfTables(pdfData: ByteArray): String {
    val inputStream = ByteArrayInputStream(pdfData)
    val extractor = ObjectExtractor(inputStream)
    val tables = mutableListOf<List<List<String?>>>()
    
    for (page: Page in extractor.extract()) {
        page.extractTables().forEach { table ->
            val rows = table.rows.map { row -> row.map { cell -> cell?.text } }
            tables.add(rows)
        }
    }
    
    extractor.close()
    return tables.toString()
}

之后可直接在Kotlin中调用该函数,或通过Chaquopy的Java-Python互调能力传递结果到Python代码中。

内容的提问来源于stack exchange,提问作者mranderson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 19:14:55