refactor(packaging): PyInstaller资源路径统一适配get_resource_path
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import base64
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
@ -19,6 +20,15 @@ from docx.shared import Inches, Pt, Cm
|
||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||
from docx.enum.section import WD_SECTION
|
||||
from docx.oxml.ns import qn
|
||||
|
||||
|
||||
def get_resource_path(relative_path: str) -> str:
|
||||
"""获取资源的绝对路径,适配 PyInstaller 打包环境。"""
|
||||
if hasattr(sys, '_MEIPASS'):
|
||||
return os.path.join(sys._MEIPASS, relative_path)
|
||||
return os.path.abspath(
|
||||
os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), relative_path)
|
||||
)
|
||||
from docx.oxml import OxmlElement
|
||||
from docx.shared import RGBColor
|
||||
import pandas as pd
|
||||
@ -848,8 +858,8 @@ class WaterQualityReportGenerator:
|
||||
section.different_first_page_header_footer = True
|
||||
|
||||
# 1. 左上角图片(增大) - 使用相对路径
|
||||
cover_top_img_path = Path(__file__).parent.parent.parent / "data" / "icons" / "word" / "lica.png"
|
||||
if cover_top_img_path.exists():
|
||||
cover_top_img_path = get_resource_path("data/icons/word/lica.png")
|
||||
if os.path.isfile(cover_top_img_path):
|
||||
try:
|
||||
p = doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.LEFT
|
||||
@ -897,8 +907,8 @@ class WaterQualityReportGenerator:
|
||||
|
||||
|
||||
# 4. 底部图片(增大) - 使用相对路径
|
||||
cover_bottom_img_path = Path(__file__).parent.parent.parent / "data" / "icons" / "word" / "fenmian.png"
|
||||
if cover_bottom_img_path.exists():
|
||||
cover_bottom_img_path = get_resource_path("data/icons/word/fenmian.png")
|
||||
if os.path.isfile(cover_bottom_img_path):
|
||||
try:
|
||||
p = doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
@ -960,8 +970,8 @@ class WaterQualityReportGenerator:
|
||||
|
||||
|
||||
# 第一张图片 - 使用相对路径
|
||||
img1_path = Path(__file__).parent.parent.parent / "data" / "icons" / "word" / "屏幕截图 2026-03-31 144131.png"
|
||||
if img1_path.exists():
|
||||
img1_path = get_resource_path("data/icons/word/屏幕截图 2026-03-31 144131.png")
|
||||
if os.path.isfile(img1_path):
|
||||
p = doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.add_run().add_picture(str(img1_path), width=Inches(6.0))
|
||||
@ -999,8 +1009,8 @@ class WaterQualityReportGenerator:
|
||||
self._style_heading(h, level=1)
|
||||
|
||||
# 插入图片 - 使用相对路径
|
||||
processing_img_path = Path(__file__).parent.parent.parent / "data" / "icons" / "word" / "liucheng.png"
|
||||
if processing_img_path.exists():
|
||||
processing_img_path = get_resource_path("data/icons/word/liucheng.png")
|
||||
if os.path.isfile(processing_img_path):
|
||||
p = doc.add_paragraph()
|
||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
p.add_run().add_picture(str(processing_img_path), width=Inches(6.5))
|
||||
@ -1356,8 +1366,8 @@ class WaterQualityReportGenerator:
|
||||
header_para = header.add_paragraph()
|
||||
|
||||
# 1. 最左侧图片 - 使用相对路径
|
||||
header_img_path = Path(__file__).parent.parent.parent / "data" / "icons" / "word" / "lica.png"
|
||||
if header_img_path.exists():
|
||||
header_img_path = get_resource_path("data/icons/word/lica.png")
|
||||
if os.path.isfile(header_img_path):
|
||||
try:
|
||||
run_img = header_para.add_run()
|
||||
run_img.add_picture(str(header_img_path), width=Inches(1.6))
|
||||
|
||||
Reference in New Issue
Block a user