1. 安装 LimeReport
步骤 1: 安装 Qt
LimeReport 是基于 Qt 的,因此首先需要安装 Qt。你可以从 Qt 官方网站下载并安装 Qt:
- 访问 Qt 下载页面。
- 选择适合你操作系统的版本。
- 安装过程中选择你需要的 Qt 版本和组件。
步骤 2: 安装 LimeReport
LimeReport 可以从其 GitHub 仓库获取源代码并编译安装,或者使用预编译的二进制文件。
使用预编译的二进制文件
- 访问 LimeReport 发布页面。
- 下载适合你操作系统的预编译二进制文件。
- 解压下载的文件,并按照提供的安装说明进行安装。
从源代码编译安装
- 克隆 LimeReport 仓库:
git clone https://github.com/LimeReport/LimeReport.git cd LimeReport - 安装依赖项(如 Qt 开发库):
sudo apt-get install qtbase5-dev libqt5svg5-dev - 编译 LimeReport:
qmake make sudo make install
2. 创建 LimeReport 的 Python 绑定
为了在 Python 中调用 LimeReport,我们需要创建一个 Python 绑定。这里我们使用 PyBind11 来创建绑定。
步骤 1: 安装 PyBind11
pip install pybind11
步骤 2: 创建绑定代码
创建一个 C++ 文件 limerreport.cpp,内容如下:
#include <pybind11/pybind11.h>
#include <limerreport/limerreport.h>
namespace py = pybind11;
void generateReport(const std::string& templatePath, const std::string& outputPath) {
LimeReport::ReportEngine engine;
if (engine.loadReport(templatePath)) {
engine.renderToFile(outputPath);
} else {
throw std::runtime_error("Failed to load report template");
}
}
PYBIND11_MODULE(limerreport, m) {
m.def("generateReport", &generateReport, "Generate a report from a template file.");
}
步骤 3: 编译绑定代码
创建一个 CMakeLists.txt 文件,内容如下:
cmake_minimum_required(VERSION 3.12)
project(LimeReportBinding)
find_package(Python REQUIRED COMPONENTS Development)
find_package(PyBind11 REQUIRED)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
include_directories(${PyBind11_INCLUDE_DIRS})
include_directories(${Qt5Core_INCLUDE_DIRS})
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_library(limerreport MODULE limerreport.cpp)
target_link_libraries(limerreport PRIVATE PyBind11::module Qt5::Core Qt5::Widgets)
set_target_properties(limerreport PROPERTIES PREFIX "")
然后编译:
mkdir build
cd build
cmake ..
make
3. 在 PyQt 应用程序中使用 LimeReport
步骤 1: 安装 PyQt5
pip install pyqt5
步骤 2: 创建 PyQt 应用程序
创建一个 Python 文件 main.py,内容如下:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
import limerreport # 导入我们创建的绑定模块
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt with LimeReport Example")
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
self.button = QPushButton("Generate Report")
self.button.clicked.connect(self.generate_report)
layout.addWidget(self.button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def generate_report(self):
try:
limerreport.generateReport("path/to/report/template.lrt", "output/report.pdf")
print("Report generated successfully.")
except Exception as e:
print(f"Error generating report: {e}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
4. 运行应用程序
确保你的环境变量中包含了 LimeReport 的库路径,然后运行应用程序:
python main.py
总结
通过以上步骤,你可以在 PyQt 应用程序中成功集成和调用 LimeReport。主要步骤包括安装 Qt 和 LimeReport,创建 LimeReport 的 Python 绑定,以及在 PyQt 应用程序中使用这些绑定来生成报表。希望这些步骤对你有所帮助!如果有任何问题,请随时提问。
564






