diff --git a/config.py b/config.py index 47fd014..121ca86 100644 --- a/config.py +++ b/config.py @@ -8,6 +8,10 @@ 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') +output_docx_path = os.path.join(output_dir, 'generated_report.docx') + +report_html_path = os.path.join(template_dir, 'report_template.html') -report_html_path = os.path.join(template_dir, 'report_template.html') \ No newline at end of file + +image_path = os.path.join(template_dir, 'image.png') \ No newline at end of file diff --git a/output/generated_report.docx b/output/generated_report.docx new file mode 100644 index 0000000..e8774d6 Binary files /dev/null and b/output/generated_report.docx differ diff --git a/output/generated_report.html b/output/generated_report.html index 97179f6..79b496d 100644 --- a/output/generated_report.html +++ b/output/generated_report.html @@ -213,12 +213,12 @@

图片展示

- 图片1 + 图片1

这是一张示例图片1

- 图片2 + 图片2

这是一张示例图片2

diff --git a/output/generated_report.pdf b/output/generated_report.pdf index c13a37d..4118c10 100644 Binary files a/output/generated_report.pdf and b/output/generated_report.pdf differ diff --git a/pdf_generation.py b/report_generation.py similarity index 81% rename from pdf_generation.py rename to report_generation.py index 0b88e07..32c4d0e 100644 --- a/pdf_generation.py +++ b/report_generation.py @@ -1,7 +1,8 @@ from jinja2 import Environment, FileSystemLoader -import os import pdfkit import config +import pypandoc + def render_html(template, output_file_path): # 定义报告数据 @@ -29,8 +30,8 @@ def render_html(template, output_file_path): ], 'highlight_content': '这是高亮显示的内容。', 'images': [ - {'src': '../input/image.png', 'alt': '图片1', 'caption': '这是一张示例图片1'}, - {'src': 'image2.jpg', 'alt': '图片2', 'caption': '这是一张示例图片2'} + {'src': config.image_path, 'alt': '图片1', 'caption': '这是一张示例图片1'}, + {'src': config.image_path, 'alt': '图片2', 'caption': '这是一张示例图片2'} ], 'contact': { 'name': '张三', @@ -59,7 +60,12 @@ def html2pdf(html_path, pdf_path): } pdfkit.from_file(html_path, pdf_path, options=options) - print("报告生成成功!") + print("pdf报告生成成功!") + +def html2docx(html_path, docx_path): + # 将HTML文件转换为WORD + pypandoc.convert_file(html_path, 'docx', outputfile=docx_path) + print("Word报告生成成功!") def main(): env = Environment(loader=FileSystemLoader(config.template_dir)) @@ -67,6 +73,8 @@ def main(): render_html(template, config.output_html_path) html2pdf(config.output_html_path, config.output_pdf_path) + html2docx(config.output_html_path, config.output_docx_path) + if __name__ == "__main__": main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6318b45 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +# sudo apt-get install pandoc +python-docx==1.1.2 +pypandoc==1.13 +pandas==2.2.2 +opencv-python==4.10.0.84 +openpyxl==3.1.4 +Jinja2==3.1.4 \ No newline at end of file diff --git a/utils/cv_tools.py b/utils/cv_tools.py new file mode 100644 index 0000000..a787a17 --- /dev/null +++ b/utils/cv_tools.py @@ -0,0 +1,17 @@ +import cv2 + + +def resize_image(input_path, output_path, new_width=200): + # 读取原始图片 + img = cv2.imread(input_path, cv2.IMREAD_UNCHANGED) + # 计算新的高度,保持纵横比 + height, width = img.shape[:2] + width_percent = (new_width / float(width)) + new_height = int((float(height) * width_percent)) + + # 调整图片大小 + resized_img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LANCZOS4) + + # 保存调整后的图片 + cv2.imwrite(output_path, resized_img) + print(f"图片已保存为 {output_path}")