整理代码,生成word

This commit is contained in:
dengjinlai 2024-07-26 16:42:17 +08:00
parent 609428be96
commit ee61bdf60c
7 changed files with 43 additions and 7 deletions

View File

@ -8,6 +8,10 @@ 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_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')
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')

Binary file not shown.

View File

@ -213,12 +213,12 @@
<h2>图片展示</h2> <h2>图片展示</h2>
<div class="image-container"> <div class="image-container">
<img src="../input/image.png" alt="图片1"> <img src="/home/dengjinlai/ReportGeneration/PdfGeneration/input/image.png" alt="图片1">
<p>这是一张示例图片1</p> <p>这是一张示例图片1</p>
</div> </div>
<div class="image-container"> <div class="image-container">
<img src="image2.jpg" alt="图片2"> <img src="/home/dengjinlai/ReportGeneration/PdfGeneration/input/image.png" alt="图片2">
<p>这是一张示例图片2</p> <p>这是一张示例图片2</p>
</div> </div>

Binary file not shown.

View File

@ -1,7 +1,8 @@
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
import os
import pdfkit import pdfkit
import config import config
import pypandoc
def render_html(template, output_file_path): def render_html(template, output_file_path):
# 定义报告数据 # 定义报告数据
@ -29,8 +30,8 @@ def render_html(template, output_file_path):
], ],
'highlight_content': '这是高亮显示的内容。', 'highlight_content': '这是高亮显示的内容。',
'images': [ 'images': [
{'src': '../input/image.png', 'alt': '图片1', 'caption': '这是一张示例图片1'}, {'src': config.image_path, 'alt': '图片1', 'caption': '这是一张示例图片1'},
{'src': 'image2.jpg', 'alt': '图片2', 'caption': '这是一张示例图片2'} {'src': config.image_path, 'alt': '图片2', 'caption': '这是一张示例图片2'}
], ],
'contact': { 'contact': {
'name': '张三', 'name': '张三',
@ -59,7 +60,12 @@ def html2pdf(html_path, pdf_path):
} }
pdfkit.from_file(html_path, pdf_path, options=options) 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(): def main():
env = Environment(loader=FileSystemLoader(config.template_dir)) env = Environment(loader=FileSystemLoader(config.template_dir))
@ -67,6 +73,8 @@ def main():
render_html(template, config.output_html_path) render_html(template, config.output_html_path)
html2pdf(config.output_html_path, config.output_pdf_path) html2pdf(config.output_html_path, config.output_pdf_path)
html2docx(config.output_html_path, config.output_docx_path)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

7
requirements.txt Normal file
View File

@ -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

17
utils/cv_tools.py Normal file
View File

@ -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}")