72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
from jinja2 import Environment, FileSystemLoader
|
|
import os
|
|
import pdfkit
|
|
import config
|
|
|
|
def render_html(template, output_file_path):
|
|
# 定义报告数据
|
|
report_data = {
|
|
'report_title': '电动机起动分析报告',
|
|
'sections': [
|
|
{'title': '章节一', 'content': '内容一'},
|
|
{'title': '章节二', 'content': '内容二'}
|
|
],
|
|
'static_descriptions': [
|
|
{'name': '组件1', 'description': '这是组件1的静态描述内容。'},
|
|
{'name': '组件2', 'description': '这是组件2的静态描述内容。'}
|
|
],
|
|
'dynamic_contents': ['动态内容1', '动态内容2'],
|
|
'naming_rules': '报告的命名规则包括...',
|
|
'version_management': {
|
|
'current_version': '1.0',
|
|
'description': '初始版本',
|
|
'status': '可用'
|
|
},
|
|
'classification_attributes': '电动机态势感知类',
|
|
'table_data': [
|
|
{'col1': '数据1-1', 'col2': '数据1-2', 'col3': '数据1-3'},
|
|
{'col1': '数据2-1', 'col2': '数据2-2', 'col3': '数据2-3'}
|
|
],
|
|
'highlight_content': '这是高亮显示的内容。',
|
|
'images': [
|
|
{'src': '../input/image.png', 'alt': '图片1', 'caption': '这是一张示例图片1'},
|
|
{'src': 'image2.jpg', 'alt': '图片2', 'caption': '这是一张示例图片2'}
|
|
],
|
|
'contact': {
|
|
'name': '张三',
|
|
'phone': '1234567890',
|
|
'email': 'zhangsan@example.com'
|
|
}
|
|
}
|
|
|
|
# 渲染模板
|
|
report_html = template.render(report_data)
|
|
with open(output_file_path, 'w', encoding='utf-8') as f:
|
|
f.write(report_html)
|
|
|
|
|
|
def html2pdf(html_path, pdf_path):
|
|
# 将HTML文件转换为PDF
|
|
options = {
|
|
'page-size': 'Letter',
|
|
'margin-top': '0.35in',
|
|
'margin-right': '0.75in',
|
|
'margin-bottom': '0.75in',
|
|
'margin-left': '0.75in',
|
|
'encoding': "UTF-8",
|
|
'no-outline': None,
|
|
'enable-local-file-access': None
|
|
}
|
|
|
|
pdfkit.from_file(html_path, pdf_path, options=options)
|
|
print("报告生成成功!")
|
|
|
|
def main():
|
|
env = Environment(loader=FileSystemLoader(config.template_dir))
|
|
template = env.get_template("report_template.html")
|
|
|
|
render_html(template, config.output_html_path)
|
|
html2pdf(config.output_html_path, config.output_pdf_path)
|
|
|
|
if __name__ == "__main__":
|
|
main() |