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

使用Jpype+AsposeCells报错:未找到JVM,未安装Java如何解决?

解决Jpype+AsposeCells运行时找不到JVM的问题

你编写的图片转Excel代码如下:

import  jpype     
import  asposecells     
jpype.startJVM() 
from asposecells.api import Workbook

# image path
image_name = 'screenshot.png'
image_path = fr'C:\\Users\\Bouregag Youcef\\Downloads\\Telegram Desktop\\analyses\\{image_name}'

# Export the dataframe to an Excel file
excel_name = 'table2.xlsx'
excel_path = fr'C:\\Users\\Bouregag Youcef\\Downloads\\Telegram Desktop\\analyses\\{excel_name}' 

workbook = Workbook(image_path)
workbook.save(excel_path)
jpype.shutdownJVM()

运行时出现错误:

Traceback (most recent call last):
  File "c:\Users\Bouregag Youcef\Downloads\Telegram Desktop\analyses\jpeg_to_xls.py", line 4, in <module>
    jpype.startJVM()
    ^^^^^^^^^^^^^^^^
  File "C:\Users\Bouregag Youcef\AppData\Local\Programs\Python\Python311\Lib\site-packages\jpype\_core.py", line 184, in startJVM
    jvmpath = getDefaultJVMPath()
              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bouregag Youcef\AppData\Local\Programs\Python\Python311\Lib\site-packages\jpype\_jvmfinder.py", line 74, in getDefaultJVMPath
    return finder.get_jvm_path()
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bouregag Youcef\AppData\Local\Programs\Python\Python311\Lib\site-packages\jpype\_jvmfinder.py", line 212, in get_jvm_path
    raise JVMNotFoundException("No JVM shared library file ({0}) "
jpype._jvmfinder.JVMNotFoundException: No JVM shared library file (jvm.dll) found. Try setting up the JAVA_HOME environment variable properly.

解决方案

方案1:安装Java并配置环境变量

AsposeCells的Python版本依赖Java环境,必须安装JDK才能使用:

  • 下载与Python位数匹配的JDK(建议JDK 1.8及以上,比如Python是64位就下载64位JDK)
  • 安装JDK后,记住安装路径(例如C:\Program Files\Java\jdk1.8.0_381)
  • 配置系统环境变量:
    • 新建系统变量JAVA_HOME,值为JDK的安装路径
    • 在系统变量Path中添加%JAVA_HOME%\bin和%JAVA_HOME%\jre\bin
  • 重启命令行或IDE,重新运行代码

方案2:改用纯Python库实现需求(无需Java)

如果只是将图片插入Excel文件,使用openpyxl即可:

from openpyxl import Workbook
from openpyxl.drawing.image import Image

# 图片路径
image_path = r'C:\Users\Bouregag Youcef\Downloads\Telegram Desktop\analyses\screenshot.png'
# Excel保存路径
excel_path = r'C:\Users\Bouregag Youcef\Downloads\Telegram Desktop\analyses\table2.xlsx'

# 创建工作簿和工作表
wb = Workbook()
ws = wb.active

# 将图片插入到A1单元格
img = Image(image_path)
ws.add_image(img, 'A1')

# 保存Excel文件
wb.save(excel_path)

如果需要识别图片中的表格内容并转换为可编辑的Excel表格,结合OCR工具pytesseract实现:

import pytesseract
from PIL import Image
import pandas as pd

# 若未将Tesseract添加到系统Path,需指定路径
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# 读取图片并执行OCR识别
image = Image.open(r'C:\Users\Bouregag Youcef\Downloads\Telegram Desktop\analyses\screenshot.png')
ocr_result = pytesseract.image_to_data(image, output_type=pytesseract.Output.DATAFRAME)

# 整理识别结果,过滤空文本
valid_data = ocr_result[ocr_result['text'].notna()][['left', 'top', 'text']]

# 根据坐标分组,识别行和列(可根据实际图片调整bins数量)
valid_data['row'] = pd.cut(valid_data['top'], bins=10, labels=False)
valid_data['col'] = pd.cut(valid_data['left'], bins=10, labels=False)

# 转换为表格格式并保存
table = valid_data.pivot(index='row', columns='col', values='text').fillna('')
table.to_excel(r'C:\Users\Bouregag Youcef\Downloads\Telegram Desktop\analyses\table2.xlsx', index=False)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 06:32:02