jinja2模板生成excel文件

This commit is contained in:
dengjinlai 2024-08-01 17:06:39 +08:00
parent 52a6d03899
commit bdbb616f2c
10 changed files with 313 additions and 2 deletions

View File

@ -7,11 +7,13 @@ template_dir = os.path.join(BASE_DIR, 'input')
output_dir = os.path.join(BASE_DIR, 'output') output_dir = os.path.join(BASE_DIR, 'output')
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
output_html_path = os.path.join(output_dir, 'generated_report.html') output_html_path = os.path.join(output_dir, 'generated_report.html')
output_excel_html_path = os.path.join(output_dir, 'excel_generated_report.html')
output_pdf_path = os.path.join(output_dir, 'generated_report.pdf') output_pdf_path = os.path.join(output_dir, 'generated_report.pdf')
output_docx_path = os.path.join(output_dir, 'generated_report.docx') output_docx_path = os.path.join(output_dir, 'generated_report.docx')
output_excel_path = os.path.join(output_dir, 'excel_generated_report.xlsx')
report_html_path = os.path.join(template_dir, 'report_template.html')
report_html_path = os.path.join(template_dir, 'report_template.html')
image_path = os.path.join(template_dir, 'image.png') image_path = os.path.join(template_dir, 'image.png')

54
excel_generation.py Normal file
View File

@ -0,0 +1,54 @@
from jinja2 import Environment, FileSystemLoader
import config
from process.inference import html2excel_pandas
def render_html(template, output_file_path):
sheets_data = [
{
'name': 'Personal Info',
'columns': ['Name', 'Age', 'Occupation'],
'data': [
['Alice', 30, 'Engineer'],
['Bob', 25, 'Data Scientist'],
['Charlie', 35, 'Teacher']
]
},
{
'name': 'Scores',
'columns': ['Name', 'Math', 'Science', 'English'],
'data': [
['Alice', 85, 92, 88],
['Bob', 78, 81, 86],
['Charlie', 93, 89, 91]
]
},
{
'name': 'Attendance',
'columns': ['Name', 'January', 'February', 'March'],
'data': [
['Alice', 'Present', 'Absent', 'Present'],
['Bob', 'Absent', 'Present', 'Present'],
['Charlie', 'Present', 'Present', 'Present']
]
}
]
# 渲染模板
html_content = template.render(sheets=sheets_data)
with open(output_file_path, 'w', encoding='utf-8') as f:
f.write(html_content)
def main():
env = Environment(loader=FileSystemLoader(config.template_dir))
template = env.get_template("excel_template.html")
render_html(template, config.output_excel_html_path)
html2excel_pandas(config.output_excel_html_path, config.output_excel_path)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Base Template{% endblock %}</title>
<style>
body {
font-family: Arial, sans-serif;
}
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

28
input/excel_template.html Normal file
View File

@ -0,0 +1,28 @@
{% extends "excel_base_template.html" %}
{% block title %}Excel Report{% endblock %}
{% block content %}
<h1>Excel Report</h1>
{% for sheet in sheets %}
<h2>{{ sheet.name }}</h2>
<table>
<thead>
<tr>
{% for column in sheet.columns %}
<th>{{ column }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in sheet.data %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
{% endblock %}

View File

@ -0,0 +1,190 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel Report</title>
<style>
body {
font-family: Arial, sans-serif;
}
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>Excel Report</h1>
<h2>Personal Info</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>30</td>
<td>Engineer</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
<td>Data Scientist</td>
</tr>
<tr>
<td>Charlie</td>
<td>35</td>
<td>Teacher</td>
</tr>
</tbody>
</table>
<h2>Scores</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Math</th>
<th>Science</th>
<th>English</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>85</td>
<td>92</td>
<td>88</td>
</tr>
<tr>
<td>Bob</td>
<td>78</td>
<td>81</td>
<td>86</td>
</tr>
<tr>
<td>Charlie</td>
<td>93</td>
<td>89</td>
<td>91</td>
</tr>
</tbody>
</table>
<h2>Attendance</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>January</th>
<th>February</th>
<th>March</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>Present</td>
<td>Absent</td>
<td>Present</td>
</tr>
<tr>
<td>Bob</td>
<td>Absent</td>
<td>Present</td>
<td>Present</td>
</tr>
<tr>
<td>Charlie</td>
<td>Present</td>
<td>Present</td>
<td>Present</td>
</tr>
</tbody>
</table>
</body>
</html>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,6 +2,7 @@ import aspose.words as aw
import pdfkit import pdfkit
from docx import Document from docx import Document
from docx.oxml.ns import qn from docx.oxml.ns import qn
import pandas as pd
# from docx.oxml import OxmlElement # from docx.oxml import OxmlElement
@ -69,9 +70,18 @@ def html2pdf_pdfkit(html_path, pdf_path):
print("pdf报告生成成功") print("pdf报告生成成功")
def html2excel_pandas(html_path, excel_path):
# 将HTML文件转换为EXCEL
tables = pd.read_html(html_path)
# 创建一个Excel工作簿
with pd.ExcelWriter(excel_path) as writer:
# 写入到Excel的不同工作表中
for i, df in enumerate(tables):
sheet_name = f'Sheet{i+1}'
df.to_excel(writer, sheet_name=sheet_name, index=False)
print("excel文件生成成功")