diff --git a/config.py b/config.py index 121ca86..99afedf 100644 --- a/config.py +++ b/config.py @@ -7,11 +7,13 @@ 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_excel_html_path = os.path.join(output_dir, 'excel_generated_report.html') output_pdf_path = os.path.join(output_dir, 'generated_report.pdf') 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') \ No newline at end of file diff --git a/excel_generation.py b/excel_generation.py new file mode 100644 index 0000000..86adf47 --- /dev/null +++ b/excel_generation.py @@ -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() \ No newline at end of file diff --git a/input/excel_base_template.html b/input/excel_base_template.html new file mode 100644 index 0000000..0a42e6d --- /dev/null +++ b/input/excel_base_template.html @@ -0,0 +1,27 @@ + + + + + + {% block title %}Base Template{% endblock %} + + + + {% block content %}{% endblock %} + + diff --git a/input/excel_template.html b/input/excel_template.html new file mode 100644 index 0000000..c25e069 --- /dev/null +++ b/input/excel_template.html @@ -0,0 +1,28 @@ +{% extends "excel_base_template.html" %} + +{% block title %}Excel Report{% endblock %} + +{% block content %} +

Excel Report

+{% for sheet in sheets %} +

{{ sheet.name }}

+ + + + {% for column in sheet.columns %} + + {% endfor %} + + + + {% for row in sheet.data %} + + {% for cell in row %} + + {% endfor %} + + {% endfor %} + +
{{ column }}
{{ cell }}
+{% endfor %} +{% endblock %} diff --git a/output/excel_generated_report.html b/output/excel_generated_report.html new file mode 100644 index 0000000..c72f958 --- /dev/null +++ b/output/excel_generated_report.html @@ -0,0 +1,190 @@ + + + + + + Excel Report + + + + +

Excel Report

+ +

Personal Info

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameAgeOccupation
Alice30Engineer
Bob25Data Scientist
Charlie35Teacher
+ +

Scores

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameMathScienceEnglish
Alice859288
Bob788186
Charlie938991
+ +

Attendance

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameJanuaryFebruaryMarch
AlicePresentAbsentPresent
BobAbsentPresentPresent
CharliePresentPresentPresent
+ + + + \ No newline at end of file diff --git a/output/excel_generated_report.xlsx b/output/excel_generated_report.xlsx new file mode 100644 index 0000000..94c4756 Binary files /dev/null and b/output/excel_generated_report.xlsx differ diff --git a/output/generated_report.docx b/output/generated_report.docx index ede49da..cd8928f 100644 Binary files a/output/generated_report.docx and b/output/generated_report.docx differ diff --git a/output/generated_report.pdf b/output/generated_report.pdf index 254563e..91ae754 100644 Binary files a/output/generated_report.pdf and b/output/generated_report.pdf differ diff --git a/process/inference.py b/process/inference.py index 20945d9..d91441a 100644 --- a/process/inference.py +++ b/process/inference.py @@ -2,6 +2,7 @@ import aspose.words as aw import pdfkit from docx import Document from docx.oxml.ns import qn +import pandas as pd # from docx.oxml import OxmlElement @@ -69,9 +70,18 @@ def html2pdf_pdfkit(html_path, pdf_path): 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文件生成成功!") diff --git a/excel_temp.py b/test_files/excel_temp.py similarity index 100% rename from excel_temp.py rename to test_files/excel_temp.py