上传文件至 /

dev提交基础报表生成
This commit is contained in:
dengjinlai 2024-07-25 16:53:12 +08:00
parent 1e444d7a49
commit c73f848e01
5 changed files with 274 additions and 0 deletions

129
base_template.html Normal file
View File

@ -0,0 +1,129 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ report_title }}</title>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
line-height: 1.6;
}
header {
background: #4CAF50;
color: white;
padding: 20px 0;
text-align: center;
}
header h1 {
margin: 0;
font-size: 2em;
}
nav {
background: #333;
color: white;
overflow: hidden;
}
nav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
.container {
padding: 20px;
}
h2 {
color: #4CAF50;
font-size: 1.5em;
border-bottom: 2px solid #4CAF50;
padding-bottom: 10px;
margin-bottom: 20px;
}
h3 {
color: #333;
font-size: 1.3em;
margin-bottom: 10px;
}
p {
font-size: 1em;
line-height: 1.6;
margin-bottom: 20px;
}
.section {
margin-bottom: 40px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
.highlight {
background-color: #ffff99;
}
footer {
background: #333;
color: white;
text-align: center;
padding: 10px 0;
position: relative;
width: 100%;
bottom: 0;
margin-top: 20px; /* 增加顶部边距以避免覆盖内容 */
}
.image-container {
width: 200px;
margin: 0 auto; /* 居中对齐 */
}
.image-container img {
width: 100%;
height: auto; /* 保持纵横比 */
}
</style>
</head>
<body>
<header>
<h1>{{ report_title }}</h1>
</header>
<nav>
<a href="#section1">章节划分</a>
<a href="#section2">静态描述内容</a>
<a href="#section3">动态加载内容</a>
<a href="#section4">报告命名规则</a>
<a href="#section5">版本管理</a>
<a href="#section6">分类属性</a>
<a href="#section7">表格数据</a>
<a href="#section8">高亮内容</a>
<a href="#section9">图片展示</a>
<a href="#section10">联系信息</a>
</nav>
<div class="container">
{% block content %}{% endblock %}
</div>
<footer>
<div>版本信息:{{ version_management.current_version }} - {{ version_management.description }} - {{ version_management.status }}</div>
<div>&copy; eCL3000 报告生成系统</div>
</footer>
</body>
</html>

13
config.py Normal file
View File

@ -0,0 +1,13 @@
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
template_dir = os.path.join(BASE_DIR, 'input')
output_dir = os.path.join(BASE_DIR, 'output')
os.makedirs(output_dir, exist_ok=True)
output_html_path = os.path.join(output_dir, 'generated_report.html')
output_pdf_path = os.path.join(output_dir, 'generated_report.pdf')
report_html_path = os.path.join(template_dir, 'report_template.html')

60
excel_temp.py Normal file
View File

@ -0,0 +1,60 @@
import openpyxl
from openpyxl.styles import Font, Alignment, PatternFill
from openpyxl.worksheet.table import Table, TableStyleInfo
# 创建一个新的工作簿
workbook = openpyxl.Workbook()
# 获取默认的工作表
sheet = workbook.active
sheet.title = 'Sales Data'
# 设置列宽
sheet.column_dimensions['A'].width = 20
sheet.column_dimensions['B'].width = 15
sheet.column_dimensions['C'].width = 15
sheet.column_dimensions['D'].width = 15
# 写入标题行
title_font = Font(bold=True, color="FFFFFF")
title_fill = PatternFill(start_color="0072BA", end_color="0072BA", fill_type="solid")
title_alignment = Alignment(horizontal="center", vertical="center")
title_row = ['Product', 'Sales Q1', 'Sales Q2', 'Sales Q3']
sheet.append(title_row) # 添加标题行数据
# 添加数据
data = [
('Product A', 1000, 1200, 1100),
('Product B', 800, 900, 950),
('Product C', 1100, 1000, 1200),
('Product D', 950, 1100, 1050),
]
for row in data:
sheet.append(row)
# 创建第二个工作表
sheet2 = workbook.create_sheet(title='Summary')
# 写入数据到第二个工作表
summary_data = [
('Total Sales', sum(row[1] for row in data), sum(row[2] for row in data), sum(row[3] for row in data)),
]
for row in summary_data:
sheet2.append(row)
# 创建一个Excel表格对象
table = Table(displayName="SalesTable", ref="A1:D6")
style = TableStyleInfo(name="xmlColumnPr", showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=True)
table.tableStyleInfo = style
# 将表格添加到工作表
sheet.add_table(table)
# 保存工作簿
workbook.save('example.xlsx')
print("Excel文件已生成")

BIN
image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

72
pdf_generation.py Normal file
View File

@ -0,0 +1,72 @@
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()