Pyjasper shows error (NameError: Invalid resource directory!)

Pyjasper shows error (NameError: Invalid resource directory!)

我正在尝试通过将 python 与 pyreport 库一起使用来生成 .jrxml,但是当我使用使用 Pyinstaller 从 .py 转换而来的 .exe 时它显示错误。

Pyinstaller版本是3.5,pyJasper版本是0.41,pyreportJasper版本是1.0.2,Python版本是3.7.1。

当我使用 Pycharm 到 运行 .py 文件并带有警告 "Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary." 时,它能够生成文件 (pdf,xlms)。根据此 link:,我认为这不是原因,但仅供参考。

文件名示例为“115kV线号1_2019-08-25”。

这是代码。

def createFolder(output_file, folderName):
    print("createFolder function started...")
    try:
        os.mkdir(output_file + folderName)
    except OSError:
        print("Creation of the directory %s failed." % output_file)
    else:
        print("Successfully created the directory %s." % output_file)


def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat):
    print("JasperReport function started...")
    con = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'cscs_prj',
        'port': '3306'
    }
    createFolder(output_file, folderName)
    output = output_file + folderName + "/" + fileName
    jasper = pyreportjasper.JasperPy()
    jasper.process(
        input_file,
        output_file=output,
        format_list=fileFormat,
        parameters=reportParameters,
        db_connection=con,
        locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
    )

但是,当我执行.exe时,出现如下错误。

  File "JasperReport.py", line 37, in jasperReport
  File "site-packages\pyreportjasper\jasperpy.py", line 151, in process
  File "site-packages\pyreportjasper\jasperpy.py", line 198, in execute
NameError: Invalid resource directory!
[5288] Failed to execute script Executer

你们知道错误发生的原因吗?

我发现问题出在 pyinstaller 不包含 .jar 和 jasperstarter.exe。我通过在 db_connection 中定义带有 jdbc_dir 的 .jar 目录和带有 jasper.path_executable 的 jasperstarter.exe 目录来解决这个问题.

这是下面的例子。

def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat, mySQLConfig, jasperConfig):
    print("JasperReport function started...")
    con = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'cscs_prj',
        'jdbc_dir': 'C:/SCPS_PRJ/JasperStarter/jdbc/',
        'port': '3306'

    }
    createFolder(output_file, folderName)
    output = output_file + folderName + "/" + fileName
    jasper = pyreportjasper.JasperPy()
    jasper.path_executable = "C:/SCPS_PRJ/JasperStarter/bin/"
    jasper.process(
        input_file,
        output_file=output,
        format_list=fileFormat,
        parameters=reportParameters,
        db_connection=con,
        locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
    )

为了使这个示例有效,请不要忘记将 .jar 放入 "C:/SCPS_PRJ/JasperStarter/jdbc/" 文件夹,并将 jasperstarter.exe 放入 "C:/SCPS_PRJ/JasperStarter/bin/" 文件夹。