ReportGeneration/excel_generation.py

54 lines
1.4 KiB
Python

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()