diff --git a/base_template.html b/base_template.html new file mode 100644 index 0000000..4b35afc --- /dev/null +++ b/base_template.html @@ -0,0 +1,129 @@ + + + + + + {{ report_title }} + + + +
+

{{ report_title }}

+
+ +
+ {% block content %}{% endblock %} +
+ + + diff --git a/config.py b/config.py new file mode 100644 index 0000000..47fd014 --- /dev/null +++ b/config.py @@ -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') \ No newline at end of file diff --git a/excel_temp.py b/excel_temp.py new file mode 100644 index 0000000..f7a3fa3 --- /dev/null +++ b/excel_temp.py @@ -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文件已生成!") diff --git a/image.png b/image.png new file mode 100644 index 0000000..950e1aa Binary files /dev/null and b/image.png differ diff --git a/pdf_generation.py b/pdf_generation.py new file mode 100644 index 0000000..0b88e07 --- /dev/null +++ b/pdf_generation.py @@ -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() \ No newline at end of file